Scene graph with different layouts
import javafx.application.Application; import javafx.collections.FXCollections; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ChoiceBox; import javafx.scene.control.Label; import javafx.scene.control.ListView; import javafx.scene.layout.BorderPane; import javafx.scene.layout.FlowPane; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class FruitsAndStuff extends Application { BorderPane componentLayout = new BorderPane(); public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Example GUI"); componentLayout.setPadding(new Insets(20,20,20,20)); componentLayout.setTop(createTop()); componentLayout.setCenter(createCenter()); componentLayout.setBottom(createBottom()); Scene appScene = new Scene(componentLayout,500,500); primaryStage.setScene(appScene); primaryStage.show(); } private Node createCenter() { final FlowPane listPane = new FlowPane(); listPane.setId("list_pane_center"); listPane.setHgap(100); Label listLbl = new Label("Vegetables"); ListView<String> vegetables = new ListView<String>(FXCollections.observableArrayList("Asparagus", "Beans", "Broccoli", "Cabbage" , "Carrot", "Celery", "Cucumber", "Leek", "Mushroom" , "Pepper", "Radish", "Shallot", "Spinach", "Swede" , "Turnip")); vegetables.setMaxHeight(300); vegetables.setId("vegetables_center"); listPane.getChildren().addAll(listLbl,vegetables); listPane.setVisible(false); return listPane; } private Node createTop() { final FlowPane choicePane = new FlowPane(); choicePane.setId("choice_pane_top"); choicePane.setHgap(100); Label choiceLbl = new Label("Fruits"); ChoiceBox<String> fruits = new ChoiceBox<String>(FXCollections.observableArrayList("Apple", "Apricot", "Banana" ,"Cherry", "Date", "Kiwi", "Orange", "Pear", "Strawberry")); fruits.setId("fruits_top"); choicePane.getChildren().add(choiceLbl); choicePane.getChildren().add(fruits); return choicePane; } private Node createBottom() { StackPane sp = new StackPane(); sp.setPadding(new Insets(10, 10, 10, 10)); sp.setStyle("-fx-background-color: #336699;"); Button vegFruitBut = new Button("Fruit or Veg"); vegFruitBut.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { Node choicePane = componentLayout.lookup("#choice_pane_top"); Node listPane = componentLayout.lookup("#list_pane_center"); choicePane.setVisible(!choicePane.isVisible()); listPane.setVisible(!listPane.isVisible()); } }); Button regBut = new Button("Register"); regBut.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent arg0) { Node choicePane = componentLayout.lookup("#choice_pane_top"); if (choicePane.isVisible()) { ChoiceBox<String> fruits = (ChoiceBox<String>)componentLayout.lookup("#fruits_top"); System.out.println(fruits.getSelectionModel().getSelectedItem()); } else { ListView<String> vegetables = (ListView<String>) componentLayout.lookup("#vegetables_center"); System.out.println(vegetables.getSelectionModel().getSelectedItem()); } } }); sp.getChildren().addAll(vegFruitBut,regBut); StackPane.setAlignment(vegFruitBut,Pos.CENTER_LEFT); StackPane.setAlignment(regBut, Pos.CENTER_RIGHT); return sp; } }