Skip to main content

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

JPasswordInput()
constructor
Creates a new JPasswordInput with no prompt text.
JPasswordInput passwordInput = new JPasswordInput();
JPasswordInput(String promptText)
constructor
Creates a new JPasswordInput with the specified prompt text.
JPasswordInput passwordInput = new JPasswordInput("Enter password");

Methods

createWithLabel(String labelText, boolean required)
VBox
Creates a VBox container with a label and this password input field. If required is true, adds a red asterisk (*) next to the label.
VBox passwordField = passwordInput.createWithLabel("Password", true);
container.getChildren().add(passwordField);

Properties

The component internally manages the following properties:
passwordField
PasswordField
The internal PasswordField used when password is hidden
textField
TextField
The internal TextField used when password is visible
toggleButton
Button
The button that toggles visibility, displays an eye icon
isVisible
boolean
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

input-wrapper
CSS class
Applied to the root StackPane container
form-input
CSS class
Applied to both the internal PasswordField and TextField
password-toggle-btn
CSS class
Applied to the visibility toggle button
icon-svg
CSS class
Applied to the SVG eye icon
form-label
CSS class
Applied to labels created with createWithLabel()
required-mark
CSS class
Applied to the asterisk (*) required indicator

Behavior

Toggle Visibility

The component automatically handles visibility toggling:
  1. Hidden State (default): Shows PasswordField with bullets (••••) and an eye icon
  2. Visible State: Shows TextField with plain text and an eye-off icon
  3. 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:
  1. Adding public getter methods
  2. Extending the class and making fields protected
  3. Adding event callback methods (e.g., setOnPasswordChanged(Consumer<String> callback))

See Also

Build docs developers (and LLMs) love