Sauer
Sauer

Reputation: 1469

Wizard with JavaFX Accordions

I'm writing a small configuration-wizard-style JavaFX application. I want to force the user through different steps of configuration panes. So I've chosen an accordion container with a few panels. The user is only allowed to proceed to the NEXT or PREVIOUS accordion panel, but may not jump from panel 1 to 3.

I achived this by adding the "Previous" and "Next" button under the accordion and I set the mouse transparency of the accordion to true. Everything works fine so far. The user can walk from panel 1 to 5 and back...

Unfortunately (for any strange reason) the user cannot configure nothing inside the different panels. Wait... I set the mouse transparency to "true", right... But when I set it to false, the user can jump through the panels without any order.

Any suggestions?

Upvotes: 0

Views: 474

Answers (1)

James_D
James_D

Reputation: 209358

One possible way is to set the collapsible property of all the panes to false, and to set each one to true just when it needs to be expanded or collapsed (which you can do programmatically). This is a little ugly, but here's an example:

import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class AccordionBasedWizard extends Application {

    @Override
    public void start(Stage primaryStage) {
        Accordion accordion = new Accordion();

        for (int i = 0 ; i < 5 ; i++) {
            TitledPane pane = new TitledPane("Page "+(i+1), new Label("Wizard page "+(i+1)));
            accordion.getPanes().add(pane);
            pane.setCollapsible(false);
        }


        accordion.setExpandedPane(accordion.getPanes().get(0));

        Button previousButton = new Button("Previous");
        previousButton.disableProperty().bind(accordion.expandedPaneProperty().isEqualTo(accordion.getPanes().get(0)));
        previousButton.setOnAction(e -> {
            TitledPane current = accordion.getExpandedPane();
            int index = accordion.getPanes().indexOf(current);
            TitledPane previous = accordion.getPanes().get(index - 1);
            current.setCollapsible(true);
            previous.setCollapsible(true);
            accordion.setExpandedPane(previous);
            previous.setCollapsible(false);
            current.setCollapsible(false);
        });

        Button nextButton = new Button("Next");
        nextButton.disableProperty().bind(accordion.expandedPaneProperty().isEqualTo(accordion.getPanes().get(accordion.getPanes().size()-1)));
        nextButton.setOnAction(e -> {
            TitledPane current = accordion.getExpandedPane();
            int index = accordion.getPanes().indexOf(current);
            TitledPane next = accordion.getPanes().get(index + 1);
            current.setCollapsible(true);
            next.setCollapsible(true);
            accordion.setExpandedPane(next);
            next.setCollapsible(false);
            current.setCollapsible(false);
        });

        HBox buttons = new HBox(5, previousButton, nextButton);
        buttons.setAlignment(Pos.CENTER);
        buttons.setPadding(new Insets(5));

        BorderPane root = new BorderPane(accordion);
        root.setBottom(buttons);

        primaryStage.setScene(new Scene(root, 400, 400));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

For what it's worth, I probably wouldn't try to use an Accordion as a wizard, and would just use a BorderPane, with the current "wizard page" in the center and next and previous buttons in the bottom. You can add a breadcrumb trail too if you need.

Upvotes: 2

Related Questions