PasswordField class is used to define a Password field control. PasswordField class extends TextField class and masks entered characters.
PasswordField class provides following constructor to define an instance.
public PasswordField()
In this application, I am going to build the UI controls using FXML.
How to set padding to GridPane?
<GridPane xmlns:fx="http://javafx.com/fxml" fx:controller="com.sample.app.widgets.controller.PasswordFieldDemoController"
fx:id="gridPane1"
hgap="20" vgap="20"
alignment="top_center">
<padding>
<Insets top="20" right="20" bottom="10" left = "30" />
</padding>
</GridPane>
How to set font to a label?
<Label fx:id="userNameLabel" text="Enter user name"
GridPane.rowIndex="0"
GridPane.columnIndex="0"
>
<font>
<Font name="Verdana" size="20" />
</font>
</Label>
How to set effect to a label?
<Label fx:id="resultLabel" text=""
GridPane.rowIndex="3"
GridPane.columnIndex="0"
GridPane.columnSpan="2"
>
<font>
<Font name="Verdana" size="35" />
</font>
<effect>
<DropShadow offsetX="3" offsetY="3">
<color>
<Color red="1" green="0" blue="0" opacity="0.7"/>
</color>
</DropShadow>
</effect>
</Label>
How to set action method to a button?
Using onAction attribute, you can set action method to a button.
<Button fx:id="signInButton" text="Sign In"
GridPane.rowIndex="2"
GridPane.columnIndex="0"
onAction="#loginAction"
>
<font>
<Font name="Verdana" size="20" />
</font>
</Button>
In control, you should define loginAction method like below.
@FXML
private void loginAction() {
}
Find the below working application.
passwordFieldDemo.fxml
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.effect.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.paint.*?>
<GridPane xmlns:fx="http://javafx.com/fxml" fx:controller="com.sample.app.widgets.controller.PasswordFieldDemoController"
fx:id="gridPane1"
hgap="20" vgap="20"
alignment="top_center">
<padding>
<Insets top="20" right="20" bottom="10" left = "30" />
</padding>
<Label fx:id="userNameLabel" text="Enter user name"
GridPane.rowIndex="0"
GridPane.columnIndex="0"
>
<font>
<Font name="Verdana" size="20" />
</font>
</Label>
<Label fx:id="passwordLabel" text="Enter password"
GridPane.rowIndex="1"
GridPane.columnIndex="0"
>
<font>
<Font name="Verdana" size="20" />
</font>
</Label>
<TextField fx:id="userNameTextField"
GridPane.rowIndex="0"
GridPane.columnIndex="1" />
<TextField fx:id="passwordTextField"
GridPane.rowIndex="1"
GridPane.columnIndex="1"
/>
<Button fx:id="signInButton" text="Sign In"
GridPane.rowIndex="2"
GridPane.columnIndex="0"
onAction="#loginAction"
>
<font>
<Font name="Verdana" size="20" />
</font>
</Button>
<Button fx:id="clearButton" text="Clear"
GridPane.rowIndex="2"
GridPane.columnIndex="1"
onAction="#clearAction"
>
<font>
<Font name="Verdana" size="20" />
</font>
</Button>
<Label fx:id="resultLabel" text=""
GridPane.rowIndex="3"
GridPane.columnIndex="0"
GridPane.columnSpan="2"
>
<font>
<Font name="Verdana" size="35" />
</font>
<effect>
<DropShadow offsetX="3" offsetY="3">
<color>
<Color red="1" green="0" blue="0" opacity="0.7"/>
</color>
</DropShadow>
</effect>
</Label>
</GridPane>
PasswordFieldDemoController.java
package com.sample.app.widgets.controller;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class PasswordFieldDemoController implements Initializable {
@FXML
private Label userNameLabel;
@FXML
private Label passwordLabel;
@FXML
private Label resultLabel;
@FXML
private TextField userNameTextField;
@FXML
private TextField passwordTextField;
@FXML
private Button signInButton;
@FXML
private Button clearButton;
@FXML
private void loginAction() {
String userName = userNameTextField.getText();
String password = passwordTextField.getText();
if (password == null || userName == null || userName.trim().isEmpty() || password.trim().isEmpty()) {
resultLabel.setText("Input shouldn't be empty");
return;
}
if (userName.equals("krishna") && password.equals("krishna")) {
resultLabel.setText("Welcome Krishna");
return;
}
resultLabel.setText("Wrong Credentials");
}
@FXML
private void clearAction() {
resultLabel.setText("");
userNameTextField.clear();
passwordTextField.clear();
}
@Override
public void initialize(URL location, ResourceBundle resources) {
}
}
PasswordFieldDemo.java
package com.sample.app.widgets;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class PasswordFieldDemo extends Application {
private static final String FXML_FILE = "/passwordFieldDemo.fxml";
private static final String STAGE_TITLE = "Password field Demo";
public static void main(String args[]) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = (Parent) FXMLLoader.load(this.getClass().getResource(FXML_FILE));
Scene scene = new Scene(root, 700, 400, Color.WHITE);
primaryStage.setTitle(STAGE_TITLE);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Output
Enter user name ‘krishna’
and password ‘krishna’ and click on Sign In button.
Previous Next Home
No comments:
Post a Comment