JavaPX TabPane and resizing parent stage

I have a scene with a variable list of checkboxes. When the list changes the changes, the stage must change the size. This works fine in most cases, adding all the checkboxes to the VBox, after which stage.sizeToScene () is called, and the scene is resized accordingly. However, if you put all this in a Tab in TabPane stage.sizeToScene () no longer works. I am a little fixated on why this is so. Has anyone got a good job or solution?

The following is an example code example. Just change the withTabs field to false or true to show the situation without and using TabPane.

Thanks for the help.

import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.CheckBox; import javafx.scene.control.ComboBox; import javafx.scene.control.Tab; import javafx.scene.control.TabPane; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class TabResizeError extends Application { boolean withTabs=false; public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Hello World!"); //holds variable number of check boxes VBox channelBox=new VBox(); channelBox.setSpacing(5); //create controls HBox controlsHBox=new HBox(); controlsHBox.setSpacing(5); Button btn = new Button(); btn.setText("Resize Test"); ComboBox<Integer> comboBox=new ComboBox<Integer>(); ObservableList<Integer> number=FXCollections.observableArrayList(); for (int i=0; i<32; i++){ number.add(i); } comboBox.setItems(number); comboBox.getSelectionModel().select(5); controlsHBox.getChildren().addAll(btn, comboBox); btn.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { //remove all check boxes channelBox.getChildren().removeAll(channelBox.getChildren()); //add new check boxes for (int i=0; i<comboBox.getSelectionModel().getSelectedItem(); i++){ CheckBox checkBox=new CheckBox(("Channel "+i)); channelBox.getChildren().add(checkBox); } primaryStage.sizeToScene(); } }); Pane root=null; VBox mainPane = new VBox(); mainPane.getChildren().addAll(controlsHBox,channelBox); mainPane.setSpacing(5); if (withTabs){ TabPane tabbedPane=new TabPane(); Tab tab=new Tab("TestTab"); tab.setContent(mainPane); tabbedPane.getTabs().add(tab); root=new BorderPane(tabbedPane); } else{ root = mainPane; } // root.setTop(controlsHBox); // root.setCenter(channelBox); primaryStage.setScene(new Scene(root)); primaryStage.setMinHeight(300); primaryStage.show(); } 

}

+5
source share
1 answer

Here is some information I found. Keep in mind that I'm still quite an amateur programmer.

I copied your code and tried to find a solution on how to get around this problem.

As I noticed, some strange things happened with the tab - the contents of the tab suddenly turned white, the methods that return the borders will always return the same value, and sizeToSceneMethod () just does it - sets the width and the height of the scene in accordance with what is inside it.

Now, having noticed strange things that I have broken through google, find this error, which says that it has not yet been resolved for several years.

There I found a piece of code that works, but it has this problem - the tab is removed and added every time, and you can see it visually

 private static final void changeTabPaneSize(final TabPane tabPane) { final int index = tabPane.getSelectionModel().getSelectedIndex(); if (index < 0 || tabPane.getTabs().isEmpty()) { return; } if (tabPane.getTabs().size() == 1) { final Tab tab = tabPane.getTabs().remove(0); tabPane.getTabs().add(tab); } else if (tabPane.getTabs().size() > 1 && index == 0) { tabPane.getSelectionModel().select(1); } else if (tabPane.getTabs().size() > 1 && index != 0) { tabPane.getSelectionModel().select(1); } tabPane.getSelectionModel().select(index); } 

Using this method and then calling sizeToScene worked:

 changeTabPaneSize(tabbedPane); primaryStage.sizeToScene();//method called after resetting the tab 

Also, the best option would be to set the scene size manually, even if this kind of smells, although it works:

 primaryStage.sizeToScene(); double constantFoundByTrialAndError = 70; primaryStage.setHeight(mainPane.getHeight()+constantFoundByTrialAndError); 

Edit: Now I noticed that I answered a 2-year question.

+2
source

All Articles