JavaFX TabPane和调整父级的大小

时间:2021-04-19 02:05:39

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?

我有一个带有变量复选框列表的舞台。当列表更改更改时,舞台必须调整大小。这在大多数情况下工作正常,在将所有复选框添加到VBox之后,然后调用stage.sizeToScene()并相应地调整舞台大小。但是,如果将所有这些放在TabPane中的Tab中,stage.sizeToScene()将不再有效。我有点不知道为什么会这样。任何人都有一个好的工作或解决方案?

Some example code is below. Just change the withTabs field to false or true, to show situation without and with a TabPane.

下面是一些示例代码。只需将withTabs字段更改为false或true,即可显示没有和使用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();
}

}

1 个解决方案

#1


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.

我注意到tabpane发生了一些奇怪的事情 - 标签的内容会突然变成白色,返回边界的方法总是会返回相同的值,而sizeToSceneMethod()只是这样做 - 设置宽度根据里面的内容,舞台的高度。

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:

使用此方法然后调用sizeToScene确实有效:

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.

编辑:我现在注意到我回答了一个2岁的问题。

#1


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.

我注意到tabpane发生了一些奇怪的事情 - 标签的内容会突然变成白色,返回边界的方法总是会返回相同的值,而sizeToSceneMethod()只是这样做 - 设置宽度根据里面的内容,舞台的高度。

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:

使用此方法然后调用sizeToScene确实有效:

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.

编辑:我现在注意到我回答了一个2岁的问题。