How can I implement the functionality of awt.CardLayout in my javaFX 2.0 application?

There is no CardLayout, but you can use TabPane or simply switch groups:

public void start(Stage stage) {

    VBox vbox = new VBox(5);

    Button btn = new Button("1");
    Button btn2 = new Button("2");

    final Pane cardsPane = new StackPane();
    final Group card1 = new Group(new Text(25, 25, "Card 1"));
    final Group card2 = new Group(new Text(25, 25, "Card 2"));

    btn.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent t) {
            cardsPane.getChildren().clear();
            cardsPane.getChildren().add(card1);
        }
    });

    btn2.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent t) {
            cardsPane.getChildren().clear();
            cardsPane.getChildren().add(card2);
        }
    });

    vbox.getChildren().addAll(btn, btn2, cardsPane);
    stage.setScene(new Scene(vbox));
    stage.setWidth(200);
    stage.setHeight(200);
    stage.show();

}

Leave a Comment