简体   繁体   中英

How to expand and collapse TitledPane in JavaFX

So, this is the code, that I have now:

    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);

As you can see, I have an accordion with three TitledPane s inside. What I want is that when a user clicks on a TitledPaned, then it should expand to the full height of its parent object (accordion in this case). But I do not know how to do that. This is what I have now:

在此处输入图片说明

Please, pay attention, that on the above picture even though the first panel is activated, it is not expanded. An this is what I want:

在此处输入图片说明

(I created the last picture manually). Thanks!

I think, you need to call setContent() method.

Example:

        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.............."));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM