GeezerVison is showing github.com — JavaFX files. · SF5/Source-Code-from-Tutorials@7041a47
File treeExpand file treeCollapse file tree Open diff view settings 010_extractAndValidateInput 012_choiceBoxDropDownList
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +53Lines changed: 53 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import javafx.application.Application; | | 2 | +import javafx.geometry.Insets; | | 3 | +import javafx.scene.Scene; | | 4 | +import javafx.scene.control.Button; | | 5 | +import javafx.scene.control.TextField; | | 6 | +import javafx.scene.layout.VBox; | | 7 | +import javafx.stage.Stage; | | 8 | + | | 9 | +public class Main extends Application { | | 10 | + | | 11 | + Stage window; | | 12 | + Scene scene; | | 13 | + Button button; | | 14 | + | | 15 | + public static void main(String[] args) { | | 16 | + launch(args); | | 17 | + } | | 18 | + | | 19 | + @Override | | 20 | + public void start(Stage primaryStage) throws Exception { | | 21 | + window = primaryStage; | | 22 | + window.setTitle("thenewboston"); | | 23 | + | | 24 | + //Form | | 25 | + TextField ageInput = new TextField(); | | 26 | + | | 27 | + button = new Button("Click me"); | | 28 | + button.setOnAction( e -> isInt(ageInput, ageInput.getText()) ); | | 29 | + | | 30 | + //Layout | | 31 | + VBox layout = new VBox(10); | | 32 | + layout.setPadding(new Insets(20, 20, 20, 20)); | | 33 | + layout.getChildren().addAll(ageInput, button); | | 34 | + | | 35 | + scene = new Scene(layout, 300, 250); | | 36 | + window.setScene(scene); | | 37 | + window.show(); | | 38 | + } | | 39 | + | | 40 | + //Validate age | | 41 | + private boolean isInt(TextField input, String message){ | | 42 | + try{ | | 43 | + int age = Integer.parseInt(input.getText()); | | 44 | + System.out.println("User is: " + age); | | 45 | + return true; | | 46 | + }catch(NumberFormatException e){ | | 47 | + System.out.println("Error: " + message + " is not a number"); | | 48 | + return false; | | 49 | + } | | 50 | + } | | 51 | + | | 52 | + | | 53 | +} |
Collapse file Copy file name to clipboard +57Lines changed: 57 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import javafx.application.Application; | | 2 | +import javafx.geometry.Insets; | | 3 | +import javafx.scene.Scene; | | 4 | +import javafx.scene.control.Button; | | 5 | +import javafx.scene.control.CheckBox; | | 6 | +import javafx.scene.layout.VBox; | | 7 | +import javafx.stage.Stage; | | 8 | + | | 9 | +public class Main extends Application { | | 10 | + | | 11 | + Stage window; | | 12 | + Scene scene; | | 13 | + Button button; | | 14 | + | | 15 | + public static void main(String[] args) { | | 16 | + launch(args); | | 17 | + } | | 18 | + | | 19 | + @Override | | 20 | + public void start(Stage primaryStage) throws Exception { | | 21 | + window = primaryStage; | | 22 | + window.setTitle("Bucky's Meat Subs"); | | 23 | + | | 24 | + //Checkboxes | | 25 | + CheckBox box1 = new CheckBox(); | | 26 | + CheckBox box2 = new CheckBox("Tuna"); | | 27 | + box2.setSelected(true); | | 28 | + | | 29 | + //Button | | 30 | + button = new Button("Order Now!"); | | 31 | + button.setOnAction(e -> handleOptions(box1, box2)); | | 32 | + | | 33 | + //Layout | | 34 | + VBox layout = new VBox(10); | | 35 | + layout.setPadding(new Insets(20, 20, 20, 20)); | | 36 | + layout.getChildren().addAll(box1, box2, button); | | 37 | + | | 38 | + scene = new Scene(layout, 300, 250); | | 39 | + window.setScene(scene); | | 40 | + window.show(); | | 41 | + } | | 42 | + | | 43 | + //Handle checkbox options | | 44 | + private void handleOptions(CheckBox box1, CheckBox box2){ | | 45 | + String message = "Users order:\n"; | | 46 | + | | 47 | + if(box1.isSelected()) | | 48 | + message += "Bacon\n"; | | 49 | + | | 50 | + if(box2.isSelected()) | | 51 | + message += "Tuna\n"; | | 52 | + | | 53 | + System.out.println(message); | | 54 | + } | | 55 | + | | 56 | + | | 57 | +} |
Collapse file Copy file name to clipboard +53Lines changed: 53 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import javafx.application.Application; | | 2 | +import javafx.geometry.Insets; | | 3 | +import javafx.scene.Scene; | | 4 | +import javafx.scene.control.Button; | | 5 | +import javafx.scene.control.ChoiceBox; | | 6 | +import javafx.scene.layout.VBox; | | 7 | +import javafx.stage.Stage; | | 8 | + | | 9 | +public class Main extends Application { | | 10 | + | | 11 | + Stage window; | | 12 | + Scene scene; | | 13 | + Button button; | | 14 | + | | 15 | + public static void main(String[] args) { | | 16 | + launch(args); | | 17 | + } | | 18 | + | | 19 | + @Override | | 20 | + public void start(Stage primaryStage) throws Exception { | | 21 | + window = primaryStage; | | 22 | + window.setTitle("ChoiceBox Demo"); | | 23 | + button = new Button("Click me"); | | 24 | + | | 25 | + ChoiceBox<String> choiceBox = new ChoiceBox<>(); | | 26 | + | | 27 | + //getItems returns the ObservableList object which you can add items to | | 28 | + choiceBox.getItems().add("Apples"); | | 29 | + choiceBox.getItems().add("Bananas"); | | 30 | + choiceBox.getItems().addAll("Bacon", "Ham", "Meatballs"); | | 31 | + | | 32 | + //Set a default value | | 33 | + choiceBox.setValue("Apples"); | | 34 | + | | 35 | + button.setOnAction(e -> getChoice(choiceBox)); | | 36 | + | | 37 | + VBox layout = new VBox(10); | | 38 | + layout.setPadding(new Insets(20, 20, 20, 20)); | | 39 | + layout.getChildren().addAll(choiceBox, button); | | 40 | + | | 41 | + scene = new Scene(layout, 300, 250); | | 42 | + window.setScene(scene); | | 43 | + window.show(); | | 44 | + } | | 45 | + | | 46 | + //To get the value of the selected item | | 47 | + private void getChoice(ChoiceBox<String> choiceBox){ | | 48 | + String food = choiceBox.getValue(); | | 49 | + System.out.println(food); | | 50 | + } | | 51 | + | | 52 | + | | 53 | +} |
Collapse file Copy file name to clipboard +53Lines changed: 53 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import javafx.application.Application; | | 2 | +import javafx.geometry.Insets; | | 3 | +import javafx.scene.Scene; | | 4 | +import javafx.scene.control.Button; | | 5 | +import javafx.scene.control.ComboBox; | | 6 | +import javafx.scene.layout.VBox; | | 7 | +import javafx.stage.Stage; | | 8 | + | | 9 | +public class Main extends Application { | | 10 | + | | 11 | + Stage window; | | 12 | + Scene scene; | | 13 | + Button button; | | 14 | + ComboBox<String> comboBox; | | 15 | + | | 16 | + public static void main(String[] args) { | | 17 | + launch(args); | | 18 | + } | | 19 | + | | 20 | + @Override | | 21 | + public void start(Stage primaryStage) throws Exception { | | 22 | + window = primaryStage; | | 23 | + window.setTitle("ComboBox Demo"); | | 24 | + button = new Button("Submit"); | | 25 | + | | 26 | + comboBox = new ComboBox<>(); | | 27 | + comboBox.getItems().addAll( | | 28 | + "Good Will Hunting", | | 29 | + "St. Vincent", | | 30 | + "Blackhat" | | 31 | + ); | | 32 | + | | 33 | + comboBox.setPromptText("What is your favorite movie?"); | | 34 | + button.setOnAction(e -> printMovie()); | | 35 | + | | 36 | + //ComboBoxes also generate actions if you need to get value instantly | | 37 | + comboBox.setOnAction( e -> System.out.println("User selected " + comboBox.getValue()) ); | | 38 | + | | 39 | + VBox layout = new VBox(10); | | 40 | + layout.setPadding(new Insets(20, 20, 20, 20)); | | 41 | + layout.getChildren().addAll(comboBox, button); | | 42 | + | | 43 | + scene = new Scene(layout, 300, 250); | | 44 | + window.setScene(scene); | | 45 | + window.show(); | | 46 | + } | | 47 | + | | 48 | + private void printMovie(){ | | 49 | + System.out.println(comboBox.getValue()); | | 50 | + } | | 51 | + | | 52 | + | | 53 | +} |
Collapse file Copy file name to clipboard +55Lines changed: 55 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import javafx.application.Application; | | 2 | +import javafx.collections.ObservableList; | | 3 | +import javafx.geometry.Insets; | | 4 | +import javafx.scene.Scene; | | 5 | +import javafx.scene.control.Button; | | 6 | +import javafx.scene.control.SelectionMode; | | 7 | +import javafx.scene.layout.VBox; | | 8 | +import javafx.stage.Stage; | | 9 | +import javafx.scene.control.ListView; | | 10 | + | | 11 | +public class Main extends Application { | | 12 | + | | 13 | + Stage window; | | 14 | + Scene scene; | | 15 | + Button button; | | 16 | + ListView<String> listView; | | 17 | + | | 18 | + public static void main(String[] args) { | | 19 | + launch(args); | | 20 | + } | | 21 | + | | 22 | + @Override | | 23 | + public void start(Stage primaryStage) throws Exception { | | 24 | + window = primaryStage; | | 25 | + window.setTitle("ListView Demo"); | | 26 | + button = new Button("Submit"); | | 27 | + | | 28 | + listView = new ListView<>(); | | 29 | + listView.getItems().addAll("Iron Man", "Titanic", "Contact", "Surrogates"); | | 30 | + listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); | | 31 | + | | 32 | + button.setOnAction(e -> buttonClicked()); | | 33 | + | | 34 | + VBox layout = new VBox(10); | | 35 | + layout.setPadding(new Insets(20, 20, 20, 20)); | | 36 | + layout.getChildren().addAll(listView, button); | | 37 | + | | 38 | + scene = new Scene(layout, 300, 250); | | 39 | + window.setScene(scene); | | 40 | + window.show(); | | 41 | + } | | 42 | + | | 43 | + private void buttonClicked(){ | | 44 | + String message = ""; | | 45 | + ObservableList<String> movies; | | 46 | + movies = listView.getSelectionModel().getSelectedItems(); | | 47 | + | | 48 | + for(String m: movies) | | 49 | + message += m + "\n"; | | 50 | + | | 51 | + System.out.println(message); | | 52 | + } | | 53 | + | | 54 | + | | 55 | +} |
|