How to use the PasswordField in JavaFX Tutorial
The passwordfield in JavaFX works the same as the JavaFX TextField, JavaFX PasswordField, and JavaFX TextArea. This type of text input control is like the TextField except it masks its text, meaning you cannot see the actual entered characters in the password field. Otherwise, it displays a bulleted character.
In this tutorial, I will walk you through how to use the password field in JavaFX. The password field is very easy to use and only used for entering passwords because it is only designed for passwords. This tutorial consists of creating the password field, setting the text, and getting the entered password from the password field. And I will also show you how to show the entered password.
Showing the entered password in JavaFX is a bit different, unlike Java Swing. It can easily use the setEchoChar() method to show the password easily. My idea of showing the entered password in JavaFX is a bit complex. Although, there are a lot of ways or methods to show the entered password in JavaFX.
JavaFX PasswordField Examples
The following examples below will teach you how to use the JavaFX PasswordField. Let’s start with creating the password field. The PasswordField only provides one constructor, which is a no-args constructor, meaning no parameters are allowed in the PasswordField’s constructor.
Create the PasswordField
It is easy to create the password field, you can also use the Scene Builder to create your graphical user interface easily. Otherwise, code it manually and the example code below will show you how to create the password field.
// create the PasswordField PasswordField passwordField = new PasswordField();
Show the PasswordField to the Scene Graph
If you don’t use the Scene Builder to make your GUI, you also need to know how to add the node to the scene with the layout in JavaFX. The code below will teach you how to add the password field node to the Scene Graph.
PasswordField passwordField = new PasswordField(); StackPane layout = new StackPane(passwordField); Scene scene = new Scene(layout, 400, 400); stage.setScene(scene); stage.show();
Output
SetText() and GetText() methods
These are the most important methods used in JavaFX PasswordField. The setText() method is used to set a text to the PasswordField and the getText() method is used to get the entered characters from the PasswordField in JavaFX. The code below will show you how to use these methods.
// set text PasswordField passwordField = new PasswordField(); passwordField.setText("samplepassword"); // get text System.out.println(passwordField.getText());
Output
How to show Password in JavaFX PasswordField
To show the entered password in PasswordField is somewhat easy and complex. My idea of showing the password in JavaFX is a little complex but easy. Showing the password in JavaFX is different from Java Swing. Java Swing uses the setEchoChar() method as I mentioned above. The following example code below will teach you how to show the entered password in JavaFX.
import javafx.beans.binding.Bindings; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Label; import javafx.scene.control.PasswordField; import javafx.scene.control.ToggleButton; import javafx.scene.input.KeyEvent; import java.net.URL; import java.util.ResourceBundle; public class PasswordFieldController implements Initializable { @FXML private PasswordField passwordField; @FXML private Label shownPassword; @FXML private ToggleButton toggleButton; @FXML void passwordFieldKeyTyped(KeyEvent event) { shownPassword.textProperty().bind(Bindings.concat(passwordField.getText())); } @FXML void toggleButton(ActionEvent event) { if (toggleButton.isSelected()) { shownPassword.setVisible(true); shownPassword.textProperty().bind(Bindings.concat(passwordField.getText())); toggleButton.setText("Hide"); }else{ shownPassword.setVisible(false); toggleButton.setText("Show"); } } @Override public void initialize(URL url, ResourceBundle resourceBundle) { shownPassword.setVisible(false); } }