Skip to main content

JInput

A styled text input component that extends JavaFX’s TextField with enhanced features and validation support.

Basic Usage

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

// Simple input
JInput input = new JInput();

// Input with placeholder
JInput nameInput = new JInput("Enter your name");

// Programmatically set placeholder
JInput emailInput = new JInput();
emailInput.setPromptText("user@example.com");

With Labels

// Create input with label
JInput usernameInput = new JInput("Enter username");
VBox labeledInput = usernameInput.createWithLabel("Username", false);

// Required field (shows asterisk)
JInput emailInput = new JInput("user@example.com");
VBox requiredInput = emailInput.createWithLabel("Email Address", true);

// Add to your layout
VBox form = new VBox(10);
form.getChildren().addAll(labeledInput, requiredInput);

Validation States

JInput input = new JInput("Enter value");

// Success state
input.setStatus("form-input-success");

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

// Warning state
input.setStatus("form-input-warning");

// Dark/neutral state
input.setStatus("form-input-dark");

// Remove validation state
input.setStatus(null);

Custom Styling

JInput input = new JInput("Placeholder")
    .addClass("custom-input", "large-font");

Complete Form Example

VBox form = new VBox(15);
form.setPadding(new Insets(20));

// Username field
JInput username = new JInput("Enter username");
VBox usernameField = username.createWithLabel("Username", true);

// Email field
JInput email = new JInput("user@example.com");
VBox emailField = email.createWithLabel("Email", true);

// Optional bio field
JInput bio = new JInput("Tell us about yourself");
VBox bioField = bio.createWithLabel("Bio", false);

// Submit button
JButton submit = new JButton("Submit")
    .addClass("btn-primary");

submit.setOnAction(e -> {
    // Validation example
    if (username.getText().isEmpty()) {
        username.setStatus("form-input-danger");
    } else {
        username.setStatus("form-input-success");
    }
});

form.getChildren().addAll(
    usernameField,
    emailField,
    bioField,
    submit
);

API Reference

Constructors

JInput()
constructor
Creates an empty input field
JInput(String promptText)
constructor
Creates an input with placeholder text

Methods

addClass(String... styleClasses)
JInput
Adds custom CSS style classes to the input
input.addClass("custom-class");
createWithLabel(String labelText, boolean required)
VBox
Creates a VBox container with a label and this input. Required fields show an asterisk.
VBox field = input.createWithLabel("Email", true);
setStatus(String styleClass)
JInput
Sets the validation status of the input. Valid values: form-input-success, form-input-danger, form-input-warning, form-input-dark. Pass null to remove all validation styles.
input.setStatus("form-input-success");

JPasswordInput

A password input component with a toggle button to show/hide the password text.

Basic Usage

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

// Simple password input
JPasswordInput password = new JPasswordInput();

// With placeholder
JPasswordInput passwordWithPrompt = new JPasswordInput("Enter your password");

With Label

JPasswordInput password = new JPasswordInput("Enter password");
VBox passwordField = password.createWithLabel("Password", true);

// Add to your form
VBox form = new VBox(10);
form.getChildren().add(passwordField);

Login Form Example

VBox loginForm = new VBox(15);
loginForm.setPadding(new Insets(20));
loginForm.setMaxWidth(400);

// Username
JInput username = new JInput("Username or email");
VBox usernameField = username.createWithLabel("Username", true);

// Password
JPasswordInput password = new JPasswordInput("Password");
VBox passwordField = password.createWithLabel("Password", true);

// Login button
JButton loginBtn = new JButton("Login")
    .addClass("btn-primary", "btn-lg");

loginBtn.setOnAction(e -> {
    String user = username.getText();
    String pass = password.passwordField.getText(); // Access internal field
    // Perform login...
});

loginForm.getChildren().addAll(
    usernameField,
    passwordField,
    loginBtn
);

API Reference

Constructors

JPasswordInput()
constructor
Creates a password input with no placeholder
JPasswordInput(String promptText)
constructor
Creates a password input with placeholder text

Methods

createWithLabel(String labelText, boolean required)
VBox
Creates a labeled container for the password input
VBox field = password.createWithLabel("Password", true);

Properties

passwordField
PasswordField
Access to the internal PasswordField for getting the password value
String passwordValue = password.passwordField.getText();
textField
TextField
Access to the internal TextField used when password is visible

JSearchInput

An advanced search input with autocomplete/suggestions support.

Basic Usage

import com.jjarroyo.components.JSearchInput;
import java.util.List;

// Simple search input
JSearchInput search = new JSearchInput();
search.setPromptText("Search...");

With Static Suggestions

JSearchInput search = new JSearchInput();
search.setPromptText("Search countries...");

List<String> countries = List.of(
    "United States", "Canada", "Mexico",
    "United Kingdom", "France", "Germany",
    "Spain", "Italy", "Japan", "China"
);

search.setSuggestions(countries);

With Dynamic Suggestions

JSearchInput search = new JSearchInput();
search.setPromptText("Search users...");

// Dynamic provider that fetches results
search.setSuggestionProvider(query -> {
    if (query == null || query.isEmpty()) {
        return List.of();
    }
    // Fetch from database, API, etc.
    return database.searchUsers(query);
});

Handling Search Events

JSearchInput search = new JSearchInput();
search.setPromptText("Search products...");

search.setOnSearch(query -> {
    System.out.println("Searching for: " + query);
    // Perform search logic
    performProductSearch(query);
});

Complete Search Example

VBox searchContainer = new VBox(10);

JSearchInput search = new JSearchInput();
search.setPromptText("Search documentation...");

// Static list of documentation pages
List<String> docPages = List.of(
    "Getting Started",
    "Installation",
    "Components Overview",
    "Theming Guide",
    "API Reference",
    "Examples"
);

search.setSuggestions(docPages);

search.setOnSearch(selectedPage -> {
    System.out.println("Navigating to: " + selectedPage);
    // Navigate to the selected page
    navigateToPage(selectedPage);
});

// Optional: Disable clear button
search.setClearable(false);

searchContainer.getChildren().add(search);

API Reference

Constructors

JSearchInput()
constructor
Creates a new search input component

Methods

setPromptText(String text)
JSearchInput
Sets the placeholder text
search.setPromptText("Search...");
getText()
String
Returns the current search text
String query = search.getText();
setText(String text)
JSearchInput
Sets the search text programmatically
search.setText("JavaFX");
setClearable(boolean clearable)
JSearchInput
Enables or disables the clear button (default: true)
search.setClearable(true);
setSuggestionProvider(Function<String, List<String>> provider)
JSearchInput
Sets a dynamic suggestion provider that receives the current query and returns matching suggestions
search.setSuggestionProvider(query -> fetchResults(query));
setSuggestions(List<String> staticSuggestions)
JSearchInput
Sets a static list of suggestions that will be filtered based on user input
search.setSuggestions(List.of("Option 1", "Option 2"));
setOnSearch(Consumer<String> callback)
JSearchInput
Sets the callback to execute when a search is committed (Enter key or suggestion selected)
search.setOnSearch(query -> System.out.println(query));
getTextField()
TextField
Returns the internal TextField for advanced customization
TextField field = search.getTextField();

Keyboard Navigation

JSearchInput

  • Enter - Commit current search query
  • Escape - Close suggestions popup
  • Down Arrow - Navigate to first suggestion
  • Up/Down - Navigate through suggestions
  • Click - Select suggestion

Form Validation Example

public class FormValidator {
    public static boolean validateEmail(JInput input) {
        String email = input.getText();
        if (email.matches("^[A-Za-z0-9+_.-]+@(.+)$")) {
            input.setStatus("form-input-success");
            return true;
        } else {
            input.setStatus("form-input-danger");
            return false;
        }
    }
    
    public static boolean validateRequired(JInput input) {
        if (input.getText().isEmpty()) {
            input.setStatus("form-input-danger");
            return false;
        } else {
            input.setStatus("form-input-success");
            return true;
        }
    }
}

Build docs developers (and LLMs) love