JavaFx Tutorial For Beginners 10 - Create Login Application
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/application/Login.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
package application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class MainController {
@FXML
private Label lblStatus;
@FXML
private TextField txtUserName;
@FXML
private TextField txPassword;
public void Login(ActionEvent event) throws Exception {
if (txtUserName.getText().equals("user") && txPassword.getText().equals("pass")) {
lblStatus.setText("Login Success");
Stage primaryStage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} else {
lblStatus.setText("Login Failed");
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="300.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
<children>
<Button layoutX="124.0" layoutY="242.0" mnemonicParsing="false" onAction="#Login" text="Login">
<font>
<Font size="18.0" />
</font>
</Button>
<TextField fx:id="txtUserName" layoutX="51.0" layoutY="98.0" promptText="UserName">
<font>
<Font size="18.0" />
</font>
</TextField>
<PasswordField fx:id="txPassword" layoutX="51.0" layoutY="150.0" promptText="Password">
<font>
<Font size="18.0" />
</font>
</PasswordField>
<Label fx:id="lblStatus" layoutX="12.0" layoutY="42.0" prefHeight="25.0" prefWidth="267.0" text="Status" textFill="#d02d1b">
<font>
<Font size="18.0" />
</font>
</Label>
</children>
</AnchorPane>
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="300.0" prefWidth="300.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.40">
<children>
<Label layoutX="106.0" layoutY="113.0" text="Main Window">
<font>
<Font size="18.0" />
</font>
</Label>
</children>
</AnchorPane>