javafxscrollbarscrollpanegridpane

JavaFX - Different behavior of two GridPane in equal component hierarchy


I believe that the properties are defined equally in both. In the "correct.fxml" grid, if I add 1 row or 1 column, the respective ScrollPane's scrollbar appears. In the "wrong.fxml" grid this does not happen and I can't figure out why. Maybe I'm not seeing something different between them. Thanks for any help.

Correct.fxml

enter image description here

Wrong.fxml

enter image description here

Correct.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.StackPane?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="429.0" 
prefWidth="761.0" stylesheets="@stylesheet.css" xmlns="http://javafx.com/javafx/22" 
xmlns:fx="http://javafx.com/fxml/1">
   <center>
      <ScrollPane BorderPane.alignment="CENTER">
         <content>
            <StackPane>
               <children>
                  <Pane StackPane.alignment="CENTER">
                     <children>
                        <GridPane alignment="CENTER" hgap="1.0" vgap="1.0">
                          <columnConstraints>
                            <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
                            <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
                              <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
                              <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
                             <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
                              <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
                              <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
                              <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
                              <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
                              <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
                              <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
                              <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
                              <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
                              <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
                              <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
                              <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
                              <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
                              <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="100.0" />
                          </columnConstraints>
                          <rowConstraints>
                            <RowConstraints maxHeight="40.0" minHeight="40.0" 
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
                              <RowConstraints maxHeight="40.0" minHeight="40.0" 
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
                              <RowConstraints maxHeight="40.0" minHeight="40.0" 
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
                              <RowConstraints maxHeight="40.0" minHeight="40.0" 
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
                              <RowConstraints maxHeight="40.0" minHeight="40.0" 
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
                              <RowConstraints maxHeight="40.0" minHeight="40.0" 
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
                              <RowConstraints maxHeight="40.0" minHeight="40.0" 
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
                              <RowConstraints maxHeight="40.0" minHeight="40.0" 
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
                              <RowConstraints maxHeight="40.0" minHeight="40.0" 
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
                              <RowConstraints maxHeight="40.0" minHeight="40.0" 
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
                          </rowConstraints>
                        </GridPane>
                     </children>
                  </Pane>
               </children>
            </StackPane>
         </content>
      </ScrollPane>
   </center>
</BorderPane>

Wrong.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.StackPane?>

<BorderPane prefHeight="277.0" prefWidth="495.0" xmlns="http://javafx.com/javafx/22" 
xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="br.com.ablogic.crossword.MainViewController">
   <center>
      <ScrollPane fx:id="scpGrid" fitToHeight="true" fitToWidth="true" 
focusTraversable="false" BorderPane.alignment="CENTER">
         <content>
            <StackPane fx:id="stkPane">
               <children>
                  <Pane fx:id="pane" StackPane.alignment="CENTER">
                     <children>
                        <GridPane alignment="CENTER" hgap="1.0" vgap="1.0">
                          <columnConstraints>
                            <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
                              <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
                              <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
                              <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
                              <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
                              <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
                              <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
                              <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
                              <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
                              <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
                              <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
                              <ColumnConstraints halignment="CENTER" hgrow="NEVER" 
maxWidth="40.0" minWidth="40.0" prefWidth="40.0" />
                          </columnConstraints>
                          <rowConstraints>
                            <RowConstraints maxHeight="40.0" minHeight="40.0" 
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
                            <RowConstraints maxHeight="40.0" minHeight="40.0" 
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
                            <RowConstraints maxHeight="40.0" minHeight="40.0" 
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
                              <RowConstraints maxHeight="40.0" minHeight="40.0" 
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
                              <RowConstraints maxHeight="40.0" minHeight="40.0" 
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
                              <RowConstraints maxHeight="40.0" minHeight="40.0" 
prefHeight="40.0" valignment="CENTER" vgrow="NEVER" />
                          </rowConstraints>
                        </GridPane>
                     </children>
                 </Pane>
              </children>
           </StackPane>
        </content>
     </ScrollPane>
  </center>
</BorderPane>

Solution

  • In the "incorrect" version, you have fitToWidth="true" and fitToHeight="true" on the declaration of your ScrollPane.

    This will force the scroll pane's content to fit the width of the viewport, and fit the height of the viewport, so that it can never be larger than the viewport and consequently scroll bars can never appear. See the documentation for these properties.

    The "correct" version of your FXML doesn't have these settings, so the content will (usually) be sized to its preferred size, allowing the scroll bars to appear when necessary.

    There are a lot of other anomalies in the code. For example, some of your column constraints have minWidth="40" maxWidth="40" prefWidth="100", which obviously makes no sense. It is typically not a good idea to hard-code these sizes (or, indeed, the size of the BorderPane, which is also different between your two versions) anyway: use percentWidth if you want to force all columns to be the same width. (There are other ways to achieve this too.) You also have completely redundant elements in the FXML in the Pane and StackPane surrounding the GridPane.