JavaFX PasswordField

This a tutorial on the JavaFX PasswordField component.

When asking your user to enter sensitive information such as passwords or PIN numbers, privacy and security is a major concern. In these cases, instead of the regular TextField component, we use the PasswordField Class.

It’s just like a regular JavaFX TextField, with the same appearance and methods (such as getting and setting text). The only difference is that when you enter anything into it, it will appear as a black dot, regardless of what you entered. You’ll see examples of this below.

The JavaFX PasswordField component is represented by the javafx.scene.control.PasswordField Class .


JavaFX PasswordField Example

To create a passwordfield component, we simply call the JavaFX PasswordField Class to create an object, then insert into the layout manager that we created.

We set a maximum width for the JavaFX PasswordField to prevent it form expanding to the same size of the window.

package application;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.layout.VBox;


public class Tutorial extends Application  {
	public static void main(String args[]){          
		 launch(args);     
	} 
		
	@Override     
	public void start(Stage primaryStage) throws Exception { 

		PasswordField password = new PasswordField();
		password.setMaxWidth(120);
	
		VBox layout = new VBox(password);
		layout.setPadding(new Insets(20, 20, 20, 20));	
     
		Scene scene = new Scene(layout, 200, 200);	

		primaryStage.setTitle("Coders - JavaFX PasswordField");
		primaryStage.setScene(scene);	
		primaryStage.show();	
    }
}

The GUI output of the above code:

JavaFX PasswordField Example

Getting a value from PasswordField

Now that we’ve created a password field, we’ll use event-handling to return the value that the User entered. The procedure is the same as a regular textfield, using the getText() method. Once we’ve done this, we load this value into a label to display the password on the GUI.

		Label label = new Label("");
		
		PasswordField password = new PasswordField();
		password.setMaxWidth(120);
		
		password.setOnAction(e ->{
			label.setText(password.getText());
		});
	
		VBox layout = new VBox(password, label);
		layout.setPadding(new Insets(20, 20, 20, 20));	
		layout.setSpacing(10);

Remember, to trigger the event handling function we’ve assigned to the passwordfield, we have to press enter once we’re done typing the text.

Below is a screen shot of us using the running and testing the above code.

JavaFX PasswordField show text

This marks the end of the JavaFX PasswordField tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the article content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments