Adding one more view with switching.

master
Tomasz Półgrabia 2021-01-16 20:13:24 +01:00
parent 1d045a0747
commit 213820ec4b
6 changed files with 158 additions and 10 deletions

View File

@ -1,25 +1,64 @@
package ch.tpolgrabia.javashowcase.javafxdemo; package ch.tpolgrabia.javashowcase.javafxdemo;
import ch.tpolgrabia.javashowcase.javafxdemo.controllers.LoginController;
import javafx.application.Application; import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage; 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 { public class JavaFxApplication extends Application {
private Stage stage;
private FXMLLoader loader;
public static void main(String[] args) { public static void main(String[] args) {
launch(args); launch(args);
} }
@Override @Override
public void start(Stage stage) { public void start(Stage stage) {
var javaVersion = System.getProperty("java.version"); this.stage = stage;
var javaFxVersion = System.getProperty("javafx.version"); this.loader = new FXMLLoader();
var l = new Label(
String.format("Hello JavaFX %s, running on java %s.", switchToLogin();
javaFxVersion, javaVersion)); }
var scene = new Scene(new StackPane(l), 640, 480);
stage.setScene(scene); @Override
stage.show(); 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);
}
} }
} }

View File

@ -0,0 +1,4 @@
package ch.tpolgrabia.javashowcase.javafxdemo.controllers;
public class HomeController {
}

View File

@ -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();
}
}

View File

@ -5,5 +5,6 @@ module ch.tpolgrabia.javashowcase.javafx1demo {
requires javafx.fxml; requires javafx.fxml;
opens ch.tpolgrabia.javashowcase.javafxdemo to javafx.fxml; opens ch.tpolgrabia.javashowcase.javafxdemo to javafx.fxml;
opens ch.tpolgrabia.javashowcase.javafxdemo.controllers to javafx.fxml;
exports ch.tpolgrabia.javashowcase.javafxdemo; exports ch.tpolgrabia.javashowcase.javafxdemo;
} }

View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Text?>
<GridPane xmlns:fx="http://javafx.com/fxml"
fx:controller="ch.tpolgrabia.javashowcase.javafxdemo.controllers.HomeController"
alignment="CENTER"
hgap="10"
vgap="10">
<Text text="Welcome to Home"
GridPane.columnIndex="0" GridPane.rowIndex="0"
GridPane.columnSpan="2" GridPane.halignment="CENTER"
/>
<Label text="User name: "
GridPane.columnIndex="0" GridPane.rowIndex="1"
/>
<TextField
fx:id="login"
GridPane.columnIndex="1" GridPane.rowIndex="1"/>
<Label text="Password: "
GridPane.columnIndex="0" GridPane.rowIndex="2"
/>
<PasswordField
fx:id="password"
GridPane.columnIndex="1" GridPane.rowIndex="2"/>
<HBox spacing="10" alignment="bottom_center"
GridPane.columnIndex="0" GridPane.rowIndex="4"
GridPane.columnSpan="2">
<Button text="Sign In NOP"/>
</HBox>
</GridPane>

View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Text?>
<GridPane xmlns:fx="http://javafx.com/fxml"
fx:controller="ch.tpolgrabia.javashowcase.javafxdemo.controllers.LoginController"
alignment="CENTER"
hgap="10"
vgap="10">
<Text text="Welcome"
GridPane.columnIndex="0" GridPane.rowIndex="0"
GridPane.columnSpan="2" GridPane.halignment="CENTER"
/>
<Label text="User name: "
GridPane.columnIndex="0" GridPane.rowIndex="1"
/>
<TextField
fx:id="login"
GridPane.columnIndex="1" GridPane.rowIndex="1"/>
<Label text="Password: "
GridPane.columnIndex="0" GridPane.rowIndex="2"
/>
<PasswordField
fx:id="password"
GridPane.columnIndex="1" GridPane.rowIndex="2"/>
<HBox spacing="10" alignment="bottom_center"
GridPane.columnIndex="0" GridPane.rowIndex="4"
GridPane.columnSpan="2">
<Button text="Sign In"
onAction="#handleLogin"/>
</HBox>
</GridPane>