Overview
JPasswordInput is a custom JavaFX component that provides a password input field with a toggle button to show/hide the password text. It extends StackPane and internally manages both a PasswordField and a TextField for switching between hidden and visible states.
Constructor
Creates a new JPasswordInput with no prompt text. JPasswordInput passwordInput = new JPasswordInput ();
JPasswordInput(String promptText)
Creates a new JPasswordInput with the specified prompt text. The placeholder text to display when the input is empty
JPasswordInput passwordInput = new JPasswordInput ( "Enter password" );
Methods
createWithLabel(String labelText, boolean required)
Creates a VBox container with a label and this password input field. If required is true, adds a red asterisk (*) next to the label. The text to display in the label
Whether to show a required indicator (*)
A VBox containing the label and password input with 4px spacing
VBox passwordField = passwordInput . createWithLabel ( "Password" , true );
container . getChildren (). add (passwordField);
Properties
The component internally manages the following properties:
The internal PasswordField used when password is hidden
The internal TextField used when password is visible
The button that toggles visibility, displays an eye icon
Current visibility state (false = hidden, true = visible)
Accessing the Text
To access or modify the password text, you need to get the internal password field:
JPasswordInput passwordInput = new JPasswordInput ( "Password" );
// Get the password value
String password = passwordInput . passwordField . getText ();
// Listen to password changes
passwordInput . passwordField . textProperty (). addListener ((obs, old, newVal) -> {
System . out . println ( "Password changed: " + newVal);
});
// Set password programmatically
passwordInput . passwordField . setText ( "initialPassword" );
The passwordField and textField are private fields. You may need to use reflection or add public getter methods to access them directly. Consider extending the class if you need more control.
Style Classes
Applied to the root StackPane container
Applied to both the internal PasswordField and TextField
Applied to the visibility toggle button
Applied to the SVG eye icon
Applied to labels created with createWithLabel()
Applied to the asterisk (*) required indicator
Behavior
Toggle Visibility
The component automatically handles visibility toggling:
Hidden State (default) : Shows PasswordField with bullets (••••) and an eye icon
Visible State : Shows TextField with plain text and an eye-off icon
Synchronized : Both fields share the same text content via bidirectional binding
SVG Icons
The component uses SVG paths for the eye icons:
Eye icon shown when password is hidden
Eye-off icon (with slash) shown when password is visible
Usage Example
import com.jjarroyo.components.JPasswordInput;
import javafx.scene.layout.VBox;
public class SecurityForm {
public VBox createPasswordFields () {
VBox form = new VBox ( 10 );
// Password input with label
JPasswordInput passwordInput = new JPasswordInput ( "Enter password" );
VBox passwordField = passwordInput . createWithLabel ( "Password" , true );
// Confirm password input
JPasswordInput confirmInput = new JPasswordInput ( "Confirm password" );
VBox confirmField = confirmInput . createWithLabel ( "Confirm Password" , true );
// Validation example (requires accessing internal fields)
// Note: You may need to add getter methods to JPasswordInput
form . getChildren (). addAll (passwordField, confirmField);
return form;
}
}
Advanced Usage with Validation
import com.jjarroyo.components.JPasswordInput;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
public class ValidatedPasswordForm {
public VBox createForm () {
VBox form = new VBox ( 10 );
JPasswordInput passwordInput = new JPasswordInput ( "Password" );
VBox passwordField = passwordInput . createWithLabel ( "Password" , true );
Label strengthLabel = new Label ();
strengthLabel . setTextFill ( Color . GRAY );
// Note: This requires adding a public getter for passwordField
// or making passwordField protected/public
passwordInput . passwordField . textProperty (). addListener ((obs, old, newVal) -> {
int strength = calculatePasswordStrength (newVal);
switch (strength) {
case 3 :
strengthLabel . setText ( "Strong password" );
strengthLabel . setTextFill ( Color . GREEN );
break ;
case 2 :
strengthLabel . setText ( "Medium strength" );
strengthLabel . setTextFill ( Color . ORANGE );
break ;
default:
strengthLabel . setText ( "Weak password" );
strengthLabel . setTextFill ( Color . RED );
}
});
form . getChildren (). addAll (passwordField, strengthLabel);
return form;
}
private int calculatePasswordStrength ( String password ) {
if ( password . length () >= 12 && password . matches ( ".*[A-Z].*" )
&& password . matches ( ".*[0-9].*" ) && password . matches ( ".*[!@#$%^&*].*" )) {
return 3 ; // Strong
} else if ( password . length () >= 8 && password . matches ( ".*[A-Z].*" )) {
return 2 ; // Medium
}
return 1 ; // Weak
}
}
Implementation Notes
Field Access : The internal passwordField, textField, and toggleButton are private. To access them from external code, consider:
Adding public getter methods
Extending the class and making fields protected
Adding event callback methods (e.g., setOnPasswordChanged(Consumer<String> callback))
See Also