JavaFX Tutorial Index

JavaFX Tutorial JavaFX Installation JavaFX Application Structure JavaFX Architecture

JavaFX Applications Charts

JavaFX Bar Chart JavaFX Bubble Chart JavaFX Pie Chart JavaFX Scatter Chart JavaFX Area Chart JavaFX Line Chart

JavaFX 2D Shapes

JavaFX Arc JavaFX Circle JavaFX Ellipse JavaFX Line JavaFX Polygon JavaFX Rectangle JavaFX Color JavaFX Gradient Color

JavaFX 3D Shapes

JavaFX Box JavaFX Cylinder JavaFX Sphere

JavaFX Animations

JavaFX Rotate Transition JavaFX Translate Transition JavaFX Fade Transition JavaFX Fill Transition JavaFX Parallel Transition JavaFX Path Transition JavaFX Pause Transition JavaFX Scale Transition JavaFX Sequential Transition JavaFX Stroke Transition

JavaFX CSS

JavaFX ID Selector JavaFX Inline Styles JavaFX Selectors

JavaFX Effect

JavaFX Blend JavaFX Bloom JavaFX Color Adjust JavaFX Color Input JavaFX Drop Shadow JavaFX Gaussian Blur JavaFX Glow JavaFX Image Input JavaFX Inner Shadow JavaFX Light Distant JavaFX Light Point JavaFX Light Spot JavaFX Lighting JavaFX Motion Blur JavaFX Reflection JavaFX Shadow

JavaFX Layouts

JavaFX Layouts JavaFX BorderPane JavaFX GridPane JavaFX StackPane JavaFX HBox JavaFX TilePane

JavaFX Event Handling

JavaFX Event Handling JavaFX Event Filters JavaFX Convenience Methods

JavaFX Transformation

JavaFX Transformation JavaFX Scaling JavaFX Rotation JavaFX Translation JavaFX Shear

JavaFX UI

JavaFX Menu JavaFX Button JavaFX Button Styling JavaFX CheckBox JavaFX File Chooser JavaFX HyperLink JavaFX Label JavaFX UI Controls JavaFX PasswordField JavaFX ProgressBar JavaFX RadioButton JavaFX ScrollBar JavaFX Slider JavaFX TextField

JavaFX PasswordField

In the JavaFX application, to get the user information and password as input from the user, a form is created using textfields and password fields which allows the user to enter input for specific fields. In JavaFX application, javafx.scene.control.PasswordField class is used to include Password Field. We can use the Password Field to create an instance of Password Field.

JavaFX PasswordField:

Example:

import javafx.application.Application;  
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;  


public class PasswordFieldUI extends Application {  
  
    @Override  
    public void start(Stage primaryStage) throws Exception {  
      Label rno=new Label(" Enter Roll No ");  
      Label pwd = new Label(" Enter Password");  
        TextField newtf=new TextField();  
        PasswordField newpf=new PasswordField();  
        newpf.setPromptText("Enter Password");  
        Button newbtn = new Button(" Submit ");  
        GridPane pane = new GridPane();  
        pane.addRow(0, rno, newtf);  
        pane.addRow(1, pwd, newpf);  
        pane.addRow(5, newbtn);  
        Scene scene=new Scene(pane,500,300);  
        primaryStage.setScene(scene);  
        primaryStage.setTitle(" JavaFX Password Field Example ");  
        primaryStage.show();  
    }


    public static void main(String[] args) 
   {
        Application.launch(args);
    }
}

Output:

In order to create the Password Field in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.scene.Scene,  javafx.scene.control.Button, javafx.stage.Stage, javafx.scene.control.PasswordField, and javafx.scene.layout.GridPane.

Then we have created one class named PaaswordFieldUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold the button with PasswordField and Labels, a GirdPane object is created which is then passed to the Scene class object.

Label for Roll number and password is created using the constructor with label text passed to it. Then one TextField is created to get input of roll number and one Password field to get the password. The setPromptText() method is used to show text in the password field. Submit button is created using the button constructor. Then using the addRow() method respective labels and textfield are added in a row. This GridPane object is given to a scene class constructor with specified width and height. The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, "JavaFX Password Field Example”. Also, it displays label to enter roll number and password. Then we can enter the same in the textfield and password field. Also, there is one submit button at last.

JavaFX PasswordField

Retrieving the data in the PasswordField:

In the JavaFX application, all the data entered in the PasswordField can be retrieved using the getText() method. This method returns a string that can be later printed or saved in the database.

Example:

import javafx.application.Application;  
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;  
import javafx.event.ActionEvent;  
import javafx.event.EventHandler;  
  
public class PasswordFieldUI extends Application {  
  
    @Override  
    public void start(Stage primaryStage) throws Exception {  
      Label rno=new Label(" Enter Roll No ");  
      Label pwd = new Label(" Enter Password ");  
        TextField newtf=new TextField();  
        PasswordField newpf=new PasswordField();  
        newpf.setPromptText("Enter Password");  
        Button newbtn = new Button("Submit");  
        newbtn.setOnAction(e->System.out.println("You entered: Roll NO : "+newtf.getText()+""+" Password: "+newpf.getText()));  
        GridPane pane = new GridPane();  
        pane.addRow(0, rno, newtf);  
        pane.addRow(1, pwd, newpf);  
        pane.addRow(5, newbtn);  
        Scene scene=new Scene(pane,500,300);  
        primaryStage.setScene(scene);  
        primaryStage.setTitle("JavaFX Password Field Example");  
        primaryStage.show();  
    }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the Password Field and action on it in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.scene.Scene,  javafx.scene.control.Button, javafx.stage.Stage, javafx.scene.control.PasswordField, javafx.event.ActionEvent, javafx.event.EventHandler, and javafx.scene.layout.GridPane.

Then we have created one class named PaaswordFieldUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold the button with PasswordField and Labels, a GirdPane object is created which is then passed to the Scene class object.

In order to get Roll number and password, Label for Roll number and password is created. Constructor is used with text string passed in it. Two textfields one for roll number and one for taking password input is created. Submit button is created using constructor. Then using the setOnAction() method on the button, values of TextField and PasswordField is retrieved with the help of the getText() method and printed on the console.

Then using the addRow() method respective labels, passwordfield, and textfield are added in a row. This GridPane object is given to a scene class constructor with specified width and height. The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, "JavaFX Password Field Example”. Also, it displays labels to enter roll number and password. Then we can enter the same in the textfield and password field. Also, there is one submit button at last. On clicking on the submit button it will display, You entered: Roll NO : 101 Password: India@123.

JavaFX PasswordField