Reputation: 714
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.
Upvotes: 17
Views: 46636
Reputation: 41
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>
Upvotes: 4
Reputation: 3375
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.
Upvotes: 8
Reputation: 840
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);
Upvotes: 13
Reputation: 1355
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;");
Upvotes: 1
Reputation: 682
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.
Upvotes: 0