17

I have a simple GridPane showing a couple of labels and a button, but I cannot seem to get it to fit the parent StackPane. How would I go forth and make it so that it fills whatever container it is in?

    GridPane g = new GridPane();
    g.add(new Label("Categories"),0,0);
    g.add(new Label("Content"),1,0);        
    Button newCat = new Button("New Category");
    newCat.setOnAction(new EventHandler<ActionEvent>() {
        @Override public void handle(ActionEvent e) {
            System.out.println("Clicked");
        }
    });       
    g.add(newCat,0,1);
    g.setGridLinesVisible(true);
    StackPane root = new StackPane();
    root.getChildren().add(g);        
    Scene scene = new Scene(root, 300, 250);  
    primaryStage.setScene(scene);
    primaryStage.show();

UI is really not my forte and any help would be appreciated.

John Mikael Gundersen
  • 714
  • 1
  • 10
  • 25

5 Answers5

13

You should see this link, especially the part about Percentage Sizing. If you have only two columns, I have found that code to work:

GridPane grid = new GridPane();
/* your code */
ColumnConstraints column1 = new ColumnConstraints();
column1.setPercentWidth(50);
grid.getColumnConstraints().add(column1);
Videl
  • 840
  • 2
  • 7
  • 18
  • 3
    This absolutely works. I was driving crazy becouse my gridpane wasn't fitting the parent, but now it does. And a note: this works even with more thanvtwo coulums, just adjust the values accordingly! – Matteo A Apr 04 '15 at 10:22
  • Thanks, I added your note to the answer. – Videl Apr 04 '15 at 13:46
8

In case anyone will be looking for an answer, this is how it worked for me:

@Override
public void start(Stage stage) throws Exception {
    stage.setTitle("Window title");
    GridPane grid = new GridPane();

    gridSetup(grid); // Building my grid elements here (2 columns)

    // Setting columns size in percent
    ColumnConstraints column = new ColumnConstraints();
    column.setPercentWidth(30);
    grid.getColumnConstraints().add(column);

    column = new ColumnConstraints();
    column.setPercentWidth(70);
    grid.getColumnConstraints().add(column);

    grid.setPrefSize(WINDOW_WIDTH, WINDOW_HEIGHT); // Default width and height
    grid.setMaxSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE);

    Scene scene = new Scene(grid, WINDOW_WIDTH, WINDOW_HEIGHT);
    stage.setScene(scene);
    stage.show();
}

With this setup the grid takes all space of the window and resizes automatically with the window.

Eugene
  • 3,280
  • 21
  • 17
4

Let's suppose your layout have 2 columns and 3 rows, you can define the properties of size of your columns and rows one-by-one:

  <GridPane 
    xmlns:fx="http://javafx.com/fxml/1"    
    id="gridPane" 
    prefHeight="768.0" 
    prefWidth="1024.0">     

    <gridLinesVisible>true</gridLinesVisible> 
    <columnConstraints>
        <ColumnConstraints percentWidth="20"  /> 
        <ColumnConstraints percentWidth="80" />        
    </columnConstraints>

    <rowConstraints>
        <RowConstraints minHeight="50"/> 
        <RowConstraints percentHeight="80"/>  
        <RowConstraints minHeight="50"/>  
    </rowConstraints> 

</GridPane>
elidio xg
  • 41
  • 3
1

Although it's not obvious, I think your GridPane is already as big as its parent container. If you add the following 2 lines of code before "Scene scene = new Scene(root, 300, 250);", you should be able to see what's going on.

root.setStyle("-fx-background-color: yellow;");
g.setStyle("-fx-border-color: blue;");
ytw
  • 1,335
  • 2
  • 20
  • 42
  • It does not scale to parent as far as I can see – John Mikael Gundersen Feb 10 '13 at 11:16
  • GridPane doesn't know how to redistribute the additional width and height. You need to define it. Do you want the first row to take up all the extra vertical space and the second column take up all the extra horizontal space? Have you considered using a TableView instead? – ytw Feb 10 '13 at 17:16
0

Rather then putting GridPane in StackPane you should add your GridPane in HBox or VBox. It is more convinient to adjust your Gridpane. BTW you still have some methods like g.setPrefSize(arg0, arg1); to set your height and width accordingly.

Bipin Bhandari
  • 652
  • 4
  • 9
  • 23
  • I know, but by setting preferred size it doesn't scale it to parent, it just sets a size that FX will try to use, but I'll try putting it a HBox, though I have a hard time believing that would fix my issue since it doesn't re-size other items like buttons and so forth... But I'll try^^ – John Mikael Gundersen Feb 08 '13 at 23:11
  • Sorry to say so, but either HBox or VBox had any effect on the GridPanes size as fas as making it fill parent goes. – John Mikael Gundersen Feb 09 '13 at 11:16
  • @JohnMikaelGundersen try Like this and adjust your Grid Constraint Accordingly. [link] http://pastebin.com/rA0Ch8ET – Bipin Bhandari Feb 09 '13 at 15:06