PasswordField
PasswordField in JavaFX is a specialized text input control that hides the typed characters, typically used for entering passwords. Unlike a regular TextField, it masks the input with bullets (•) so that others cannot see what the user types.
Example: Simple Password Input
FXML (hello-view.fxml)
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.VBox?>
<VBox spacing="10" alignment="CENTER"
xmlns="http://javafx.com/javafx/17"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="org.example.demo.HelloController">
<PasswordField fx:id="passwordField" promptText="Enter your password"/>
<Button text="Submit" onAction="#handleSubmit"/>
</VBox>
Controller (HelloController.java)
package org.example.demo;
import javafx.fxml.FXML;
import javafx.scene.control.PasswordField;
public class HelloController {
@FXML
private PasswordField passwordField;
@FXML
private void handleSubmit() {
String password = passwordField.getText();
System.out.println("Entered password: " + password);
}
}
How It Works
PasswordFieldbehaves like a regularTextField, but the input is masked.promptTextis optional—it shows a placeholder until the user types something.- You can read the input using
passwordField.getText().
✅ Tip: PasswordField doesn’t prevent copying or programmatically reading the value—so for real security, always hash the password before storing or transmitting it.