Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javafx-2

In my javaFX 2.0 app, I need to replace a component which is used awt.CardLayout. Cardlayout has a functionality as a stack which displays the top component in stack. And also we can manually configure which is to be displayed.

In javaFX 2.0, there is a layout called StackPane. But It doesn't seems like Cardlayout.

like image 552
Anuruddha Avatar asked Nov 29 '11 11:11

Anuruddha


People also ask

Is CardLayout a class of Java AWT package?

The CardLayout class manages the components in such a way that only one component is visible at a time. It treats each component as a card in the container. Only one card is visible at a time, and the container acts as a stack of cards.

Where is the CardLayout used?

The CardLayout class manages two or more components (usually JPanel instances) that share the same display space. When using the CardLayout class, let the user choose between the components by using a combo box.

Which of the following are parameters of CardLayout ()?

Parameters: hgap - the horizontal gap. vgap - the vertical gap.


1 Answers

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

}
like image 139
Sergey Grinev Avatar answered Sep 19 '22 19:09

Sergey Grinev