JavaFX TabPane 和调整父级的大小
JavaFX TabPane and resizing parent Stage
我有一个带有可变复选框列表的阶段。当列表更改时,舞台必须调整大小。这在大多数情况下都可以正常工作,在将所有复选框添加到 VBox 之后,您然后调用 stage.sizeToScene() 并且舞台会相应地调整大小。但是,如果您将所有这些都放在 TabPane 中的 Tab 中,stage.sizeToScene() 将不再有效。我对这是为什么有点困惑。任何人都有很好的解决方法或解决方案?
下面是一些示例代码。只需将 withTabs 字段更改为 false 或 true,以显示没有和有 TabPane 的情况。
感谢您的帮助。
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();
}
}
这是我找到的一些信息。请记住,我仍然是一个业余程序员。
我复制了您的代码并试图找到解决此问题的方法。
正如我所注意到的,选项卡窗格发生了一些奇怪的事情 - 选项卡的内容会突然变白,return 边界的方法总是 return 相同值,而 sizeToSceneMethod() 就是这样做的 - 根据里面的内容设置舞台的宽度和高度。
现在在注意到奇怪的事情后,我从 google 挖掘到 find this bug submission,它说它仍然没有解决几年。
在那里我找到了一个有效的代码片段,但它有这个问题 - 每次都会删除和添加选项卡,您可以直观地看到这一点
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);
}
使用此方法然后调用 sizeToScene 确实有效:
changeTabPaneSize(tabbedPane);
primaryStage.sizeToScene();//method called after resetting the tab
除此之外,您最好的选择是手动设置舞台大小,尽管这种气味很有效:
primaryStage.sizeToScene();
double constantFoundByTrialAndError = 70;
primaryStage.setHeight(mainPane.getHeight()+constantFoundByTrialAndError);
编辑:我现在注意到我回答了一个 2 年前的问题。
我有一个带有可变复选框列表的阶段。当列表更改时,舞台必须调整大小。这在大多数情况下都可以正常工作,在将所有复选框添加到 VBox 之后,您然后调用 stage.sizeToScene() 并且舞台会相应地调整大小。但是,如果您将所有这些都放在 TabPane 中的 Tab 中,stage.sizeToScene() 将不再有效。我对这是为什么有点困惑。任何人都有很好的解决方法或解决方案?
下面是一些示例代码。只需将 withTabs 字段更改为 false 或 true,以显示没有和有 TabPane 的情况。
感谢您的帮助。
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();
}
}
这是我找到的一些信息。请记住,我仍然是一个业余程序员。
我复制了您的代码并试图找到解决此问题的方法。
正如我所注意到的,选项卡窗格发生了一些奇怪的事情 - 选项卡的内容会突然变白,return 边界的方法总是 return 相同值,而 sizeToSceneMethod() 就是这样做的 - 根据里面的内容设置舞台的宽度和高度。
现在在注意到奇怪的事情后,我从 google 挖掘到 find this bug submission,它说它仍然没有解决几年。
在那里我找到了一个有效的代码片段,但它有这个问题 - 每次都会删除和添加选项卡,您可以直观地看到这一点
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);
}
使用此方法然后调用 sizeToScene 确实有效:
changeTabPaneSize(tabbedPane);
primaryStage.sizeToScene();//method called after resetting the tab
除此之外,您最好的选择是手动设置舞台大小,尽管这种气味很有效:
primaryStage.sizeToScene();
double constantFoundByTrialAndError = 70;
primaryStage.setHeight(mainPane.getHeight()+constantFoundByTrialAndError);
编辑:我现在注意到我回答了一个 2 年前的问题。