How do i make a TabPane fill its parent?


Question

My TabPane only seems to fill it's width horizontally, but not vertically. My workaround for now is to do this:

    stage.getScene().heightProperty().addListener(new ChangeListener<Number>() {

      @Override
      public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
        System.out.println("height changed");
        tabPane.setPrefHeight(newValue.doubleValue());
      }
    });

But if i use that height it's obviously larger than the area that actually remains for it (theres a MenuBar above the TabPane). (Or does it adjust it's size properly?) It also kinda feels wrong, there got to be simple boolean i can set, since it behaves exactly as expected for horizontal changes.

The Scene is setup like this:

public static ResourceBundle BUNDLE = ResourceBundle.getBundle("locales/Bundle", new Locale("en", "GB"));
Scene scene = new Scene(new VBox(0), 800, 600);
MenuBar menuBar = new MenuBar();
Menu menuStart = new Menu(BUNDLE.getString("menu.start"));
Menu menuView = new Menu(BUNDLE.getString("menu.view"));
Menu menuHelp = new Menu(BUNDLE.getString("menu.help"));
menuBar.getMenus().addAll(menuStart, menuView, menuHelp);

((VBox) stage.getScene().getRoot()).getChildren().add(menuBar);
TabPane tabPane = new TabPane();
((VBox) stage.getScene().getRoot()).getChildren().add(tabPane);
stage.setScene(scene);
stage.show();

Ofcourse there is some more code, but it only contains drag'n'drop related listeners, and this problem occurs way before any of them ever did anything (they print to console everytime they do something).

The Tab setup:

final Tab tab = new Tab();
final Label label = new Label("Tab" +text);
tab.setGraphic(label);
StackPane pane = new StackPane();
int red = rng.nextInt(256);
int green = rng.nextInt(256);
int blue = rng.nextInt(256);
String style = String.format("-fx-background-color: rgb(%d, %d, %d);", red, green, blue);
pane.setStyle(style);
Label label = new Label("This is tab " + text);
label.setStyle(String.format("-fx-text-fill: rgb(%d, %d, %d);", 256 - red, 256 - green, 256 - blue));
pane.setPrefSize(500, 500);
pane.getChildren().add(label);
tab.setContent(pane);

Which is from How to drag and drop tab nodes between tab panes

1
3
5/23/2017 12:24:38 PM

Accepted Answer

VBox.setVgrow(tabPane, Priority.ALWAYS);

7
8/6/2014 3:30:49 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon