Skip to content

Commit

Permalink
Add loading and login screens with MaterialFX integration
Browse files Browse the repository at this point in the history
  • Loading branch information
iKaueMatos committed Dec 27, 2024
1 parent fc5b81d commit eb1afe2
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 8 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>

<groupId>com.zpl</groupId>
<artifactId>zpl</artifactId>
<artifactId>zpl-javaFX</artifactId>
<version>1.0-SNAPSHOT</version>
<name>zpl</name>

Expand All @@ -25,6 +25,11 @@
<artifactId>javafx-fxml</artifactId>
<version>17.0.6</version>
</dependency>
<dependency>
<groupId>io.github.palexdev</groupId>
<artifactId>materialfx</artifactId>
<version>11.17.0</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-web</artifactId>
Expand Down
48 changes: 45 additions & 3 deletions src/main/java/com/zpl/zpl/ZplApplication.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,58 @@
package com.zpl.zpl;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.concurrent.Task;

import com.zpl.zpl.infrastructure.database.DatabaseInitializer;
import com.zpl.zpl.ui.StageInitializer;
import com.zpl.zpl.infrastructure.http.controller.LoginScreenController;

public class ZplApplication extends Application {
@Override
public void start(@SuppressWarnings("exports") Stage primaryStage) throws Exception {
DatabaseInitializer.initialize();
StageInitializer.initialize(primaryStage);
showLoadingScreen(primaryStage);

Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
DatabaseInitializer.initialize();
return null;
}

@Override
protected void succeeded() {
try {
showLoginScreen(primaryStage);
} catch (Exception e) {
e.printStackTrace();
}
}
};

new Thread(task).start();
}

private void showLoadingScreen(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/loading_screen.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setFullScreen(true);
stage.show();
}

private void showLoginScreen(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/#_screen.fxml"));
Parent root = loader.load();
LoginScreenController controller = loader.getController();
controller.setStage(stage);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setFullScreen(false);
stage.show();
}

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.zpl.zpl.infrastructure.http.controller;

import javafx.fxml.FXML;
import javafx.scene.control.ProgressIndicator;

public class LoadingScreenController {
@FXML
private ProgressIndicator progressIndicator;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.zpl.zpl.infrastructure.http.controller;

import java.io.IOException;

import com.zpl.zpl.ui.StageInitializer;

import io.github.palexdev.materialfx.controls.MFXButton;
import io.github.palexdev.materialfx.controls.MFXPasswordField;
import io.github.palexdev.materialfx.controls.MFXTextField;
import javafx.fxml.FXML;
import javafx.stage.Stage;

public class LoginScreenController {
@FXML
private MFXTextField usernameField;
@FXML
private MFXPasswordField passwordField;
@FXML
private MFXButton loginButton;

private Stage stage;

public void setStage(Stage stage) {
this.stage = stage;
}

@FXML
private void handleLogin() throws IOException {
StageInitializer.initialize(stage);
}

@FXML
private void handleForgotPassword() {
// Add logic to handle forgotten password
}
}
40 changes: 40 additions & 0 deletions src/main/java/com/zpl/zpl/ui/#Screen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.zpl.zpl.ui;

import io.github.palexdev.materialfx.controls.MFXButton;
import io.github.palexdev.materialfx.controls.MFXPasswordField;
import io.github.palexdev.materialfx.controls.MFXTextField;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.IOException;

public class LoginScreen {
private Stage stage;

public LoginScreen(Stage stage) {
this.stage = stage;
}

public void show() {
VBox root = new VBox(10);
MFXTextField usernameField = new MFXTextField();
usernameField.setPromptText("Username");
MFXPasswordField passwordField = new MFXPasswordField();
passwordField.setPromptText("Password");
MFXButton loginButton = new MFXButton("Login");

loginButton.setOnAction(event -> {
try {
StageInitializer.initialize(stage);
} catch (IOException e) {
throw new RuntimeException(e);
}
});

root.getChildren().addAll(usernameField, passwordField, loginButton);
Scene scene = new Scene(root, 400, 300);
stage.setScene(scene);
stage.show();
}
}
5 changes: 1 addition & 4 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
module com.zpl.zpl {
requires javafx.controls;
requires javafx.fxml;
requires javafx.web;
requires org.apache.poi.poi;
requires org.apache.poi.ooxml;
requires java.sql;
requires java.desktop;

requires org.controlsfx.controls;
requires com.dlsc.formsfx;
requires net.synedra.validatorfx;
requires org.kordamp.ikonli.javafx;
requires org.kordamp.bootstrapfx.core;
requires com.almasb.fxgl.all;
requires usb.api;
requires MaterialFX;

opens com.zpl.zpl to javafx.fxml;
exports com.zpl.zpl;
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/view/loading_screen.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.control.ProgressIndicator?>

<StackPane xmlns:fx="http://javafx.com/fxml" fx:controller="com.zpl.zpl.ui.LoadingScreenController">
<ProgressIndicator />
</StackPane>
26 changes: 26 additions & 0 deletions src/main/resources/view/#_screen.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.VBox?>
<?import javafx.geometry.Insets?>
<?import io.github.palexdev.materialfx.controls.MFXButton?>
<?import io.github.palexdev.materialfx.controls.MFXPasswordField?>
<?import io.github.palexdev.materialfx.controls.MFXTextField?>
<?import javafx.scene.paint.Color?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>

<VBox xmlns:fx="http://javafx.com/fxml" fx:controller="com.zpl.zpl.ui.LoginScreenController" spacing="15" alignment="CENTER" style="-fx-background-color: #f4f4f9; -fx-padding: 20;">

<Label text="Login" style="-fx-font-size: 24px; -fx-font-weight: bold; -fx-text-fill: #4a90e2;" />

<MFXTextField fx:id="usernameField" promptText="Username" style="-fx-font-size: 14px; -fx-border-radius: 5px; -fx-background-radius: 5px;" />

<MFXPasswordField fx:id="passwordField" promptText="Password" style="-fx-font-size: 14px; -fx-border-radius: 5px; -fx-background-radius: 5px;" />

<MFXButton text="Login" onAction="#handleLogin" style="-fx-font-size: 16px; -fx-background-color: #4a90e2; -fx-text-fill: white; -fx-border-radius: 5px;" />

<HBox alignment="CENTER" spacing="5">
<Label text="Forgot your password?" style="-fx-font-size: 12px; -fx-text-fill: #4a90e2; -fx-cursor: hand;" onMouseClicked="#handleForgotPassword"/>
</HBox>
</VBox>

0 comments on commit eb1afe2

Please # to comment.