如何在JavaFX中展开和折叠TitledPane

时间:2015-11-27 18:51:36

标签: java javafx

所以,这是我现在拥有的代码:

    SplitPane sp = new SplitPane();
    final StackPane sp1 = new StackPane();

    final Accordion accordion = new Accordion (); 

    TitledPane tp1 = new TitledPane();
    tp1.setText("First");
    TitledPane tp2 = new TitledPane();
    tp2.setText("Second");
    TitledPane tp3 = new TitledPane();
    tp3.setText("Third");

    accordion.getPanes().add(tp1);
    accordion.getPanes().add(tp2);
    accordion.getPanes().add(tp3);


    sp1.getChildren().add(accordion);

    final StackPane sp2 = new StackPane();
    sp.getItems().addAll( sp1 , sp2 );        
    double height = Screen.getPrimary().getVisualBounds().getHeight();
    double width = Screen.getPrimary().getVisualBounds().getWidth();
    Scene scene = new Scene( sp );
    primaryStage.setScene( scene );        
    primaryStage.setMaximized( true );
    primaryStage.setMinHeight( height );
    primaryStage.setMinWidth( width );        
    primaryStage.show();
    sp.setDividerPositions(0.3);

如你所见,我手风琴里面有三个TitledPane。我想要的是当用户点击TitledPaned时,它应该扩展到其父对象的完整高度(在这种情况下是accordion)。但我不知道该怎么做。这就是我现在所拥有的:

enter image description here

请注意,即使第一个面板被激活,在上面的图片上也没有扩展。这就是我想要的:

enter image description here

(我手动创建了最后一张图片)。谢谢!

1 个答案:

答案 0 :(得分:1)

我认为,您需要调用setContent()方法。

示例:

        TextField firstNameFld = new TextField();
        firstNameFld.setPrefColumnCount(8);
        TextField lastNameFld = new TextField();
        lastNameFld.setPrefColumnCount(8);
        GridPane grid = new GridPane();
        grid.addRow(0, new Label("First Name:"), firstNameFld);
        grid.addRow(1, new Label("Last Name:"), lastNameFld); 
        tp1.setContent(grid);
        tp2.setContent(new Label("tp2............."));
        tp3.setContent(new Label("tp3.............."));