Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX TabPane and resizing parent Stage

I have a stage with a variable list of check boxes. When the list changes changes the stage has to resize. This works fine on most occasions, after adding all check boxes to a VBox you then call stage.sizeToScene() and the stage resizes accordingly. However, if you put all this in a Tab within a TabPane stage.sizeToScene() no longer works. I'm a bit stuck as to why this is. Anyone got a good work around or solution?

Some example code is below. Just change the withTabs field to false or true, to show situation without and with a 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();
}

}

like image 231
macster110 Avatar asked Oct 31 '22 04:10

macster110


1 Answers

Here is some info that I found. Keep in mind that I am still quite an amateur programmer.

I copied your code and tried to find a solution on how to work around this issue.

There were some weird things happening with the tabpane as I noticed - the contents of the tab would turn white all of a sudden, the methods that would return bounds would always return the same value, and sizeToSceneMethod() just does that - sets the width and height of the stage according to what's inside it.

Now after noticing strange things I dug through google to find this bug submission, which says it is still has not been resolved for a few years.

There I found a code snippet which works, but it has this problem - tab is removed and added every time, and you can see this 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 did work:

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

Other than that, your best bet would be to set stage size manually, even though this kind of smells, though it works:

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

Edit: I now noticed that I answered a 2 year old question.

like image 53
Arūnas Rimkus Avatar answered Nov 15 '22 06:11

Arūnas Rimkus