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.
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.
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.
Parameters: hgap - the horizontal gap. vgap - the vertical gap.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With