Dimitris Sfounis
Dimitris Sfounis

Reputation: 2500

JavaFX: Making BorderPane's Center's contents not strech

just a quick one for you since I'm generally inexperienced in designing apps. Any help would be greatly appreciated.

I've got the following application, and it's obvious that it uses a BorderPane for its layout. I've got a GridPane of 5 labels and 5 textFields in the Center region, and I wanted a subtle border for it.

screenshot

As you can see, the GridPane utilizes the full space allocated to it in the Center region, and although the border gets drawn nicely, it goes all the way down to the bottom of the Center region (Red arrows). What I want it to do is finish at the blue line.

I tried using grid.setPrefHeight(400);, for example, but it didn't work.

Are there any solutions other than the obvious one, to add a second container below the GridPane and squeeze the upper container enough?

EDIT: For reference, this is the code that creates the center area GridPane:

public GridPane addGridPaneCenter() {
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(0, 10, 0, 10));

sihuid = new Text("SIHU ID:");
sihuid.setFont(Font.font("Inconsolata", 16));
grid.add(sihuid, 0, 1);                                           //spans from 0,1 to 1,1 (Column-Row)
sihuid_tf = new TextField();
sihuid_tf.setEditable(false);
grid.add(sihuid_tf, 1, 1);

mac = new Text("Plug MAC:");
mac.setFont(Font.font("Inconsolata", 16));
grid.add(mac, 0, 2);
mac_tf = new TextField();
mac_tf.setEditable(false);
grid.add(mac_tf, 1, 2);

loc = new Text("Location:");
loc.setFont(Font.font("Inconsolata", 16));
grid.add(loc, 0, 3);
loc_tf = new TextField();
loc_tf.setEditable(false);
grid.add(loc_tf, 1, 3);

appl = new Text("Appliance:");
appl.setFont(Font.font("Inconsolata", 16));
grid.add(appl, 0, 4);
appl_tf = new TextField();
appl_tf.setEditable(false);
grid.add(appl_tf, 1, 4);

type = new Text("Type:");
type.setFont(Font.font("Inconsolata", 16));
grid.add(type, 0, 5);
type_tf = new TextField();
type_tf.setEditable(false);
grid.add(type_tf, 1, 5);

grid.setPrefHeight(400);
grid.setStyle(
    "-fx-border-color: #b8b8ba; -fx-border-width: 1;"
    + "-fx-border-radius: 4;"
    /*+ "-fx-font: " + "Inconsolata" + ";" */
); 
return grid;

}

After that, there's a simple

GridPane grid_center = addGridPaneCenter();                             //CENTER Grid, contains info about plugs.
        border_pane.setCenter(grid_center);

    Scene scene = new Scene(border_pane, 900, 700);
    scene.setFill(Color.GHOSTWHITE);

    primaryStage.setTitle("PlugControl v0.1e");
    primaryStage.setScene(scene);
    primaryStage.show();

Upvotes: 1

Views: 1765

Answers (1)

Nikos Paraskevopoulos
Nikos Paraskevopoulos

Reputation: 40298

I'm afraid this is behaviour of BorderPane is by design (ref):

The top and bottom children will be resized to their preferred heights and extend the width of the borderpane. The left and right children will be resized to their preferred widths and extend the length between the top and bottom nodes. And the center node will be resized to fill the available space in the middle.

You should probably put the GridPane in an AnchorPane or VBox and put that (AnchorPane or VBox) in the center of the BorderPane.

Upvotes: 1

Related Questions