GeezerVison is showing github.com — Added more JavaFX tutorials. · 4CGO/Source-Code-from-Tutorials@8596a87
File treeExpand file treeCollapse file tree Open diff view settings 006_communicatingBetweenWindows
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +46Lines changed: 46 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import javafx.application.*; | | 2 | +import javafx.stage.*; | | 3 | +import javafx.scene.*; | | 4 | +import javafx.scene.layout.*; | | 5 | +import javafx.scene.control.*; | | 6 | + | | 7 | +public class Main extends Application { | | 8 | + | | 9 | + Stage window; | | 10 | + Scene scene1, scene2; | | 11 | + | | 12 | + public static void main(String[] args) { | | 13 | + launch(args); | | 14 | + } | | 15 | + | | 16 | + @Override | | 17 | + public void start(Stage primaryStage) { | | 18 | + window = primaryStage; | | 19 | + | | 20 | + //Button 1 | | 21 | + Label label1 = new Label("Welcome to the first scene!"); | | 22 | + Button button1 = new Button("Go to scene 2"); | | 23 | + button1.setOnAction(e -> window.setScene(scene2)); | | 24 | + | | 25 | + //Layout 1 - children laid out in vertical column | | 26 | + VBox layout1 = new VBox(20); | | 27 | + layout1.getChildren().addAll(label1, button1); | | 28 | + scene1 = new Scene(layout1, 200, 200); | | 29 | + | | 30 | + | | 31 | + //Button 2 | | 32 | + Button button2 = new Button("This sucks, go back to scene 1"); | | 33 | + button2.setOnAction(e -> window.setScene(scene1)); | | 34 | + | | 35 | + //Layout 2 | | 36 | + StackPane layout2 = new StackPane(); | | 37 | + layout2.getChildren().add(button2); | | 38 | + scene2 = new Scene(layout2, 600, 300); | | 39 | + | | 40 | + //Display scene 1 at first | | 41 | + window.setScene(scene1); | | 42 | + window.setTitle("Title Here"); | | 43 | + window.show(); | | 44 | + } | | 45 | + | | 46 | +} |
Collapse file Copy file name to clipboard +32Lines changed: 32 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import javafx.stage.*; | | 2 | +import javafx.scene.*; | | 3 | +import javafx.scene.layout.*; | | 4 | +import javafx.scene.control.*; | | 5 | +import javafx.geometry.*; | | 6 | + | | 7 | +public class AlertBox { | | 8 | + | | 9 | + public static void display(String title, String message) { | | 10 | + Stage window = new Stage(); | | 11 | + | | 12 | + //Block events to other windows | | 13 | + window.initModality(Modality.APPLICATION_MODAL); | | 14 | + window.setTitle(title); | | 15 | + window.setMinWidth(250); | | 16 | + | | 17 | + Label label = new Label(); | | 18 | + label.setText(message); | | 19 | + Button closeButton = new Button("Close this window"); | | 20 | + closeButton.setOnAction(e -> window.close()); | | 21 | + | | 22 | + VBox layout = new VBox(10); | | 23 | + layout.getChildren().addAll(label, closeButton); | | 24 | + layout.setAlignment(Pos.CENTER); | | 25 | + | | 26 | + //Display window and wait for it to be closed before returning | | 27 | + Scene scene = new Scene(layout); | | 28 | + window.setScene(scene); | | 29 | + window.showAndWait(); | | 30 | + } | | 31 | + | | 32 | +} |
Collapse file Copy file name to clipboard +31Lines changed: 31 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import javafx.application.Application; | | 2 | +import javafx.scene.Scene; | | 3 | +import javafx.scene.control.Button; | | 4 | +import javafx.scene.layout.StackPane; | | 5 | +import javafx.stage.Stage; | | 6 | + | | 7 | +public class Main extends Application { | | 8 | + | | 9 | + Stage window; | | 10 | + Button button; | | 11 | + | | 12 | + public static void main(String[] args) { | | 13 | + launch(args); | | 14 | + } | | 15 | + | | 16 | + @Override | | 17 | + public void start(Stage primaryStage) { | | 18 | + window = primaryStage; | | 19 | + window.setTitle("thenewboston"); | | 20 | + button = new Button("Click Me"); | | 21 | + | | 22 | + button.setOnAction(e -> AlertBox.display("Title of Window", "Wow this alert box is awesome!")); | | 23 | + | | 24 | + StackPane layout = new StackPane(); | | 25 | + layout.getChildren().add(button); | | 26 | + Scene scene = new Scene(layout, 300, 250); | | 27 | + window.setScene(scene); | | 28 | + window.show(); | | 29 | + } | | 30 | + | | 31 | +} |
Collapse file Copy file name to clipboard +47Lines changed: 47 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import javafx.stage.*; | | 2 | +import javafx.scene.*; | | 3 | +import javafx.scene.layout.*; | | 4 | +import javafx.scene.control.*; | | 5 | +import javafx.geometry.*; | | 6 | + | | 7 | +public class ConfirmBox { | | 8 | + | | 9 | + //Create variable | | 10 | + static boolean answer; | | 11 | + | | 12 | + public static boolean display(String title, String message) { | | 13 | + Stage window = new Stage(); | | 14 | + window.initModality(Modality.APPLICATION_MODAL); | | 15 | + window.setTitle(title); | | 16 | + window.setMinWidth(250); | | 17 | + Label label = new Label(); | | 18 | + label.setText(message); | | 19 | + | | 20 | + //Create two buttons | | 21 | + Button yesButton = new Button("Yes"); | | 22 | + Button noButton = new Button("No"); | | 23 | + | | 24 | + //Clicking will set answer and close window | | 25 | + yesButton.setOnAction(e -> { | | 26 | + answer = true; | | 27 | + window.close(); | | 28 | + }); | | 29 | + noButton.setOnAction(e -> { | | 30 | + answer = false; | | 31 | + window.close(); | | 32 | + }); | | 33 | + | | 34 | + VBox layout = new VBox(10); | | 35 | + | | 36 | + //Add buttons | | 37 | + layout.getChildren().addAll(label, yesButton, noButton); | | 38 | + layout.setAlignment(Pos.CENTER); | | 39 | + Scene scene = new Scene(layout); | | 40 | + window.setScene(scene); | | 41 | + window.showAndWait(); | | 42 | + | | 43 | + //Make sure to return answer | | 44 | + return answer; | | 45 | + } | | 46 | + | | 47 | +} |
Collapse file Copy file name to clipboard +34Lines changed: 34 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import javafx.application.Application; | | 2 | +import javafx.scene.Scene; | | 3 | +import javafx.scene.control.Button; | | 4 | +import javafx.scene.layout.StackPane; | | 5 | +import javafx.stage.Stage; | | 6 | + | | 7 | +public class Main extends Application { | | 8 | + | | 9 | + Stage window; | | 10 | + Button button; | | 11 | + | | 12 | + public static void main(String[] args) { | | 13 | + launch(args); | | 14 | + } | | 15 | + | | 16 | + @Override | | 17 | + public void start(Stage primaryStage) { | | 18 | + window = primaryStage; | | 19 | + window.setTitle("JavaFX - thenewboston"); | | 20 | + button = new Button("Click Me"); | | 21 | + | | 22 | + button.setOnAction(e -> { | | 23 | + boolean result = ConfirmBox.display("Title of Window", "Are you sure you want to send that pic?"); | | 24 | + System.out.println(result); | | 25 | + }); | | 26 | + | | 27 | + StackPane layout = new StackPane(); | | 28 | + layout.getChildren().add(button); | | 29 | + Scene scene = new Scene(layout, 300, 250); | | 30 | + window.setScene(scene); | | 31 | + window.show(); | | 32 | + } | | 33 | + | | 34 | +} |
|