Skip to content

Instantly share code, notes, and snippets.

@sharifulislam52
Last active May 4, 2018 16:49
Show Gist options
  • Save sharifulislam52/7e79ab60e4cb71aef23e122e09cbc659 to your computer and use it in GitHub Desktop.
Save sharifulislam52/7e79ab60e4cb71aef23e122e09cbc659 to your computer and use it in GitHub Desktop.
javafx : Login example with image view.
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
Stage window;
@Override
public void start(Stage primaryStage) {
window = primaryStage;
try {
Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
Scene scene = new Scene(root,600,300);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public void close_window() {
window.close();
}
public static void main(String[] args) {
launch(args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="300.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
<children>
<TextField fx:id="username" layoutX="38.0" layoutY="90.0" promptText="Username">
<font>
<Font size="18.0" />
</font>
</TextField>
<PasswordField fx:id="password" layoutX="39.0" layoutY="150.0" promptText="Password">
<font>
<Font size="18.0" />
</font>
</PasswordField>
<Label fx:id="status" alignment="CENTER" layoutX="35.0" layoutY="47.0" prefHeight="27.0" prefWidth="224.0" text="Status" textFill="#191cdd">
<font>
<Font size="18.0" />
</font>
</Label>
<Button layoutX="112.0" layoutY="224.0" mnemonicParsing="false" onAction="#login">
<font>
<Font size="18.0" />
</font>
<graphic>
<ImageView fitHeight="100.0" fitWidth="150.0" layoutX="330.0" layoutY="75.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="img/loginBtn.jpg" />
</image></ImageView>
</graphic>
</Button>
<ImageView fitHeight="150.0" fitWidth="200.0" layoutX="330.0" layoutY="75.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="img/login.png" />
</image></ImageView>
</children>
</AnchorPane>
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 {
Main m = new Main();
@FXML
private Label status;
@FXML
private TextField username;
@FXML
private TextField password;
public void login(ActionEvent event) {
if(username.getText().equals("sharif") && password.getText().equals("123")) {
status.setText("Login Success!");
try {
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
Scene scene = new Scene(root,300,300);
Stage primaryStage = new Stage();
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
else {status.setText("Login not Success!");}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane minHeight="300.0" prefWidth="300.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.171">
<children>
<Label alignment="CENTER" layoutX="9.0" layoutY="133.0" prefHeight="17.0" prefWidth="281.0" text="Main Window">
<font>
<Font size="18.0" />
</font>
</Label>
</children>
</AnchorPane>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment