Skip to main content

Overview

JInput is a customized JavaFX TextField with built-in styling and validation support. It extends TextField and provides additional methods for styling, validation states, and label creation.

Constructor

JInput()
constructor
Creates a new JInput with no prompt text.
JInput input = new JInput();
JInput(String promptText)
constructor
Creates a new JInput with the specified prompt text.
JInput input = new JInput("Enter your name");

Methods

addClass(String... styleClasses)
JInput
Adds custom CSS style classes to the input.
input.addClass("custom-class", "another-class");
createWithLabel(String labelText, boolean required)
VBox
Creates a VBox container with a label and this input field. If required is true, adds a red asterisk (*) next to the label.
VBox formField = input.createWithLabel("Username", true);
container.getChildren().add(formField);
setStatus(String styleClass)
JInput
Sets the validation status of the input by applying a status style class.
// Show success state
input.setStatus("form-input-success");

// Show error state
input.setStatus("form-input-danger");

// Clear status
input.setStatus(null);

Style Classes

form-input
CSS class
Base style class automatically applied to all JInput instances
form-label
CSS class
Style class applied to labels created with createWithLabel()
required-mark
CSS class
Style class applied to the asterisk (*) required indicator

Usage Example

import com.jjarroyo.components.JInput;
import javafx.scene.layout.VBox;

public class LoginForm {
    public VBox createForm() {
        VBox form = new VBox(10);
        
        // Basic input with label
        JInput usernameInput = new JInput("Enter username");
        VBox usernameField = usernameInput.createWithLabel("Username", true);
        
        // Input with custom styling
        JInput emailInput = new JInput("user@example.com")
            .addClass("email-input");
        VBox emailField = emailInput.createWithLabel("Email", true);
        
        // Input with validation
        JInput passwordInput = new JInput("Password");
        passwordInput.textProperty().addListener((obs, old, newVal) -> {
            if (newVal.length() < 8) {
                passwordInput.setStatus("form-input-danger");
            } else {
                passwordInput.setStatus("form-input-success");
            }
        });
        VBox passwordField = passwordInput.createWithLabel("Password", true);
        
        form.getChildren().addAll(usernameField, emailField, passwordField);
        return form;
    }
}

Inherited Methods

Since JInput extends TextField, all standard JavaFX TextField methods are available:
  • setText(String text) - Set the text content
  • getText() - Get the current text
  • setPromptText(String value) - Set placeholder text
  • setEditable(boolean value) - Enable/disable editing
  • setDisable(boolean value) - Enable/disable the input
  • textProperty() - Access the text property for binding
  • And all other TextField methods

See Also

Build docs developers (and LLMs) love