我试图获得手风琴的UI行为,其中用户可以弹出任何TitledPane
到窗口,并将窗口弹回到手风琴内的TitledPane
。将标题窗口内容弹出窗口
但是,当弹出折叠的TitledPane
时,内容未在Stage
中正确对齐,并且如果no窗格展开,它甚至不会显示。
附加是显示问题的最小示例 - 请注意,我保留了两个占位符窗格,以避免内容节点(我的情况下为A VBox
)多次出现在场景图中。我曾尝试在VBox
上设置preferredSize
和visible
属性,并在显示前后调用layout
,甚至以编程方式展开标题窗格,但似乎没有任何效果。结果
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
TitledPane t1 = new TitledPane();
TitledPane t2 = new TitledPane();
Accordion accordion = new Accordion(t1, t2);
t1.setContent(buildComponent("Pane 1", t1, accordion));
t2.setContent(buildComponent("Pane 2", t2, accordion));
primaryStage.setScene(new Scene(accordion, 300, 300));
primaryStage.show();
}
private VBox buildComponent(String name, TitledPane titledPane, Accordion holder) {
final Button popout = new Button("Pop out");
titledPane.setGraphic(popout);
titledPane.setText(name);
final VBox component = new VBox(new Label(name), new TableView<>());
final Pane placeholder1 = new Pane();
final Pane placeholder2 = new Pane();
Stage st = new Stage();
st.setScene(new Scene(placeholder1, 300, 300));
popout.setOnAction(event -> {
if (!st.equals(component.getScene().getWindow())) {
holder.getPanes().remove(titledPane);
titledPane.setContent(placeholder2);
st.getScene().setRoot(component);
st.show();
}
});
st.setOnHidden(windowEvent -> {
st.getScene().setRoot(placeholder1);
titledPane.setContent(component);
holder.getPanes().add(titledPane);
});
return component;
}
public static void main(String[] args) {
launch(args);
}
}
插图:
时候,没有窗格中展开:当其他窗格中展开
结果。注意标签是多么的不可见:当窗格中展开
结果 - 这是我想在所有情况下,结果: