Prevent an accordion in JavaFX from collapsing

痴心易碎 提交于 2019-12-21 09:22:06

问题


Is there an easy way of preventing an accordion in JavaFX 2.1 from fully collapsing? I have an accordion with a few entries but if the user clicks the active accordion entry it collapses the accordion.

I could probably use a mouse click listener to check do the check and act accordingly but this feels like it should be even simpler than that to accomplish.


回答1:


Add a listener to the currently expanded accordion pane and prevent it from being collapsed by the user by modifying it's collapsible property.

Here is a sample app:

import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class AccordionSample extends Application {
  public static void main(String[] args) { launch(args); }
  @Override public void start(Stage primaryStage) {
    // create some titled panes to go in an accordion.
    TitledPane adminPane = new TitledPane("Animals", 
      VBoxBuilder.create().style("-fx-padding: 10").spacing(10).children(
        ButtonBuilder.create().text("Zebra").maxWidth(Double.MAX_VALUE).build(),
        ButtonBuilder.create().text("Shrew").maxWidth(Double.MAX_VALUE).build()
      ).build()            
    );
    TitledPane viewPane = new TitledPane("Vegetables", 
      VBoxBuilder.create().style("-fx-padding: 10").spacing(10).children(
        ButtonBuilder.create().text("Eggplant").maxWidth(Double.MAX_VALUE).build(),
        ButtonBuilder.create().text("Carrot").maxWidth(Double.MAX_VALUE).build()
      ).build()            
    );

    // create an accordion, ensuring the currently expanded pane can not be clicked on to collapse.
    Accordion accordion = new Accordion();
    accordion.getPanes().addAll(adminPane, viewPane);
    accordion.expandedPaneProperty().addListener(new ChangeListener<TitledPane>() {
      @Override public void changed(ObservableValue<? extends TitledPane> property, final TitledPane oldPane, final TitledPane newPane) {
        if (oldPane != null) oldPane.setCollapsible(true);
        if (newPane != null) Platform.runLater(new Runnable() { @Override public void run() { 
          newPane.setCollapsible(false); 
        }});
      }
    });
    for (TitledPane pane: accordion.getPanes()) pane.setAnimated(false);
    accordion.setExpandedPane(accordion.getPanes().get(0));

    // layout the scene.
    StackPane layout = new StackPane();
    layout.setStyle("-fx-padding: 10; -fx-background-color: cornsilk;");
    layout.getChildren().add(accordion);
    primaryStage.setScene(new Scene(layout));
    primaryStage.show();
  }
}



回答2:


Here is another solution for making sure the accordion will never completely collapse. The difference from the great original answer by @jewelsea is little - I didn't like the fact that the default down facing arrow was disappearing from the open accordion TitledPane face, because its "collapsible" property is being set to false. I played with it a bit more to achieve a more "natural" feel for my interface.

    /* Make sure the accordion can never be completely collapsed */
    accordeon.expandedPaneProperty().addListener((ObservableValue<? extends TitledPane> observable, TitledPane oldPane, TitledPane newPane) -> {
        Boolean expand = true; // This value will change to false if there's (at least) one pane that is in "expanded" state, so we don't have to expand anything manually
        for(TitledPane pane: accordeon.getPanes()) {
            if(pane.isExpanded()) {
                expand = false;
            }
        }
        /* Here we already know whether we need to expand the old pane again */
        if((expand == true) && (oldPane != null)) {
            Platform.runLater(() -> {
                accordeon.setExpandedPane(oldPane);
            });
        }
    });


来源:https://stackoverflow.com/questions/10403838/prevent-an-accordion-in-javafx-from-collapsing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!