protected CheckBox ckbBurger, ckbDrink, ckbFries; The Gui above is built with the method below. Note, in the highlighted line below, that the containing the HBox is constructed by the method above, and is added at (0,0) in the CheckBoxes buildFoodSelection, GridPane: private GridPane buildGuiPane() { GridPane grid = new GridPane(); grid.setHgap(10); grid.setVgap(10); grid.setPadding(new Insets(25, 25, 25, 25)); grid.add(buildFoodSelection(), 0, 0); Button btnProcess = new Button("Process"); btnProcess.setOnAction(new ProcessEventHandler()); grid.add(btnProcess, 0, 1); txaMessage = new TextArea(); grid.add(txaMessage, 0, 2); return grid; } Button Event Handler We use the method to see which is selected. – isSelected CheckBox private class ProcessEventHandler implements EventHandler<ActionEvent> { @Override public void handle(ActionEvent event) { double cost = 0.0; String order = "Order:\n"; if(ckbBurger.isSelected()) { cost += 5.99; order += "Burger"; } if(ckbDrink.isSelected()) { cost += 1.99; order += ", Drink"; } if(ckbFries.isSelected()) { cost += 1.49; order += ", Fries"; } String totCost = String.format("\nTotal: $%,.2f\n", cost); order += totCost; } } RadioButton, VBox, & Nested Panes Example – The Gui on the right utilizes a as the root pane. At position (0,0) it uses a . The is indentical to an GridPane VBox except that the controls (and/or other panes) are stacked vertically. HBox
protected CheckBox ckbBurger, ckbDrink, ckbFries; The Gui above is built with the method below. Note, in the highlighted line below, that the HBox containing the CheckBoxes is constructed by the method above, buildFoodSelection, and is added at (0,0) in the GridPane: private GridPane buildGuiPane() { GridPane grid = new GridPane(); grid.setHgap(10); grid.setVgap(10); grid.setPadding(new Insets(25, 25, 25, 25)); grid.add(buildFoodSelection(), 0, 0); Button btnProcess = new Button("Process"); btnProcess.setOnAction(new ProcessEventHandler()); grid.add(btnProcess, 0, 1); txaMessage = new TextArea(); grid.add(txaMessage, 0, 2); return grid; } Button Event Handler – We use the isSelected method to see which CheckBox is selected. private class ProcessEventHandler implements EventHandler<ActionEvent> { @Override public void handle(ActionEvent event) { double cost = 0.0; String order = "Order:\n"; if(ckbBurger.isSelected()) { cost += 5.99; order += "Burger"; } if(ckbDrink.isSelected()) { cost += 1.99; order += ", Drink"; } if(ckbFries.isSelected()) { cost += 1.49; order += ", Fries"; } String totCost = String.format("\nTotal: $%,.2f\n", cost); order += totCost; } } – RadioButton, VBox, & Nested Panes Example The Gui on the right utilizes a GridPane as the root pane. At position (0,0) it uses a . The VBox is indentical to an HBox except that the controls (and/or other panes) are stacked vertically.
ch14_16_gui.docx