Jeremy
Jeremy

Reputation: 631

Prevent an accordion in JavaFX from collapsing

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.

Upvotes: 8

Views: 5880

Answers (2)

fault-tolerant
fault-tolerant

Reputation: 531

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

Upvotes: 5

jewelsea
jewelsea

Reputation: 159436

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

Upvotes: 7

Related Questions