From 213820ec4b2ad14c50cf52699de13649e4088a52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20P=C3=B3=C5=82grabia?= Date: Sat, 16 Jan 2021 20:13:24 +0100 Subject: [PATCH] Adding one more view with switching. --- .../javafxdemo/JavaFxApplication.java | 59 +++++++++++++++---- .../controllers/HomeController.java | 4 ++ .../controllers/LoginController.java | 25 ++++++++ javafx-demo1/src/main/java/module-info.java | 1 + javafx-demo1/src/main/resources/home.fxml | 39 ++++++++++++ javafx-demo1/src/main/resources/login.fxml | 40 +++++++++++++ 6 files changed, 158 insertions(+), 10 deletions(-) create mode 100644 javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/controllers/HomeController.java create mode 100644 javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/controllers/LoginController.java create mode 100644 javafx-demo1/src/main/resources/home.fxml create mode 100644 javafx-demo1/src/main/resources/login.fxml diff --git a/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/JavaFxApplication.java b/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/JavaFxApplication.java index 54560a9..3e67046 100644 --- a/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/JavaFxApplication.java +++ b/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/JavaFxApplication.java @@ -1,25 +1,64 @@ package ch.tpolgrabia.javashowcase.javafxdemo; +import ch.tpolgrabia.javashowcase.javafxdemo.controllers.LoginController; import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; import javafx.scene.Scene; -import javafx.scene.control.Label; -import javafx.scene.layout.StackPane; import javafx.stage.Stage; +import java.io.IOException; +import java.io.InputStream; + +// TODO add event bus like system to switch between views and add messaging public class JavaFxApplication extends Application { + private Stage stage; + private FXMLLoader loader; + public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { - var javaVersion = System.getProperty("java.version"); - var javaFxVersion = System.getProperty("javafx.version"); - var l = new Label( - String.format("Hello JavaFX %s, running on java %s.", - javaFxVersion, javaVersion)); - var scene = new Scene(new StackPane(l), 640, 480); - stage.setScene(scene); - stage.show(); + this.stage = stage; + this.loader = new FXMLLoader(); + + switchToLogin(); + } + + @Override + public void stop() { + this.stage = null; + this.loader = null; + } + + private void switchToLogin() { + try (InputStream is = getClass().getClassLoader().getResourceAsStream("login.fxml")) { + var loader = new FXMLLoader(); + Parent root = loader.load(is); + LoginController loginController = loader.getController(); + loginController.setSwitchToHome(this::switchToHome); + var scene = new Scene(root, 640, 480); + stage.setScene(scene); + stage.show(); + + } catch (IOException e) { + e.printStackTrace(); + System.exit(1); + } + } + + public void switchToHome() { + try (InputStream is = getClass().getClassLoader().getResourceAsStream("home.fxml")) { + Parent root = loader.load(is); + var scene = new Scene(root, 640, 480); + stage.setScene(scene); + stage.show(); + + } catch (IOException e) { + e.printStackTrace(); + System.exit(1); + } } } diff --git a/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/controllers/HomeController.java b/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/controllers/HomeController.java new file mode 100644 index 0000000..7d9cabf --- /dev/null +++ b/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/controllers/HomeController.java @@ -0,0 +1,4 @@ +package ch.tpolgrabia.javashowcase.javafxdemo.controllers; + +public class HomeController { +} diff --git a/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/controllers/LoginController.java b/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/controllers/LoginController.java new file mode 100644 index 0000000..934efea --- /dev/null +++ b/javafx-demo1/src/main/java/ch/tpolgrabia/javashowcase/javafxdemo/controllers/LoginController.java @@ -0,0 +1,25 @@ +package ch.tpolgrabia.javashowcase.javafxdemo.controllers; + +import javafx.fxml.FXML; +import javafx.scene.control.PasswordField; +import javafx.scene.control.TextField; + +public class LoginController { + @FXML + private TextField login; + @FXML + private PasswordField password; + + private Runnable switchToHome; + + public void setSwitchToHome(Runnable switchToHome) { + this.switchToHome = switchToHome; + } + + @FXML + public void handleLogin() { + System.out.printf("Got data login: %s, password: %s\n", + login.getText(), password.getText()); + switchToHome.run(); + } +} diff --git a/javafx-demo1/src/main/java/module-info.java b/javafx-demo1/src/main/java/module-info.java index 9ef8e9b..ba276c6 100644 --- a/javafx-demo1/src/main/java/module-info.java +++ b/javafx-demo1/src/main/java/module-info.java @@ -5,5 +5,6 @@ module ch.tpolgrabia.javashowcase.javafx1demo { requires javafx.fxml; opens ch.tpolgrabia.javashowcase.javafxdemo to javafx.fxml; + opens ch.tpolgrabia.javashowcase.javafxdemo.controllers to javafx.fxml; exports ch.tpolgrabia.javashowcase.javafxdemo; } diff --git a/javafx-demo1/src/main/resources/home.fxml b/javafx-demo1/src/main/resources/home.fxml new file mode 100644 index 0000000..40bd9df --- /dev/null +++ b/javafx-demo1/src/main/resources/home.fxml @@ -0,0 +1,39 @@ + + + + + + + + + +