Overview
JSearchInput is a sophisticated search input component with autocomplete/suggestions functionality. It extends HBox and provides a search icon, text field, clear button, and a popup for displaying suggestions.
Constructor
Creates a new JSearchInput with default configuration. JSearchInput searchInput = new JSearchInput ();
Methods
Configuration Methods
setPromptText(String text)
Sets the placeholder text for the search input. The placeholder text to display
Returns this instance for method chaining
searchInput . setPromptText ( "Search for products..." );
Sets the current text value of the search input. Returns this instance for method chaining
searchInput . setText ( "initial query" );
Gets the current text value of the search input. The current text in the search field
String query = searchInput . getText ();
setClearable(boolean clearable)
Sets whether the clear button should be shown when text is present. True to show clear button, false to hide it (default: true)
Returns this instance for method chaining
searchInput . setClearable ( false );
Suggestion Methods
setSuggestionProvider(provider)
Sets a dynamic suggestion provider that generates suggestions based on the current input text. provider
Function<String, List<String>>
A function that takes the current query string and returns a list of suggestions
Returns this instance for method chaining
searchInput . setSuggestionProvider (query -> {
// Fetch suggestions from API or database
return productService . searchProducts (query);
});
setSuggestions(List<String> staticSuggestions)
Sets a static list of suggestions that will be filtered based on the current input text. A fixed list of suggestions to filter from
Returns this instance for method chaining
List & lt;String & gt; countries = Arrays . asList ( "United States" , "Canada" , "Mexico" , "Brazil" );
searchInput . setSuggestions (countries);
setOnSearch(Consumer<String> callback)
Sets a callback to be invoked when a search is committed (Enter pressed or suggestion selected). The callback function that receives the search query
Returns this instance for method chaining
searchInput . setOnSearch (query -> {
System . out . println ( "Searching for: " + query);
performSearch (query);
});
Gets the internal TextField for advanced customization. The internal TextField component
TextField field = searchInput . getTextField ();
field . setMaxWidth ( 400 );
Keyboard Navigation
The component supports comprehensive keyboard navigation:
Commits the current search query and invokes the onSearch callback
Closes the suggestions popup
Moves focus to the first suggestion in the popup, or opens the popup if closed
Navigate between suggestions in the popup
Returns focus to the text field
Style Classes
Applied to the root HBox container
Applied to the search icon (magnifying glass)
Applied to the internal TextField
Applied to the clear button (X icon)
Applied to the VBox containing suggestions
Applied to the ScrollPane wrapping the suggestions
Applied to individual suggestion items
Usage Example
Basic Search Input
import com.jjarroyo.components.JSearchInput;
import javafx.scene.layout.VBox;
public class SearchExample {
public VBox createSearchBar () {
VBox container = new VBox ( 10 );
JSearchInput searchInput = new JSearchInput ();
searchInput . setPromptText ( "Search products..." )
. setOnSearch (query -> performSearch (query));
container . getChildren (). add (searchInput);
return container;
}
private void performSearch ( String query ) {
System . out . println ( "Searching for: " + query);
// Implement search logic
}
}
With Static Suggestions
import com.jjarroyo.components.JSearchInput;
import java.util.Arrays;
import java.util.List;
public class CountrySearch {
public JSearchInput createCountrySearch () {
List & lt;String & gt; countries = Arrays . asList (
"United States" , "Canada" , "Mexico" , "Brazil" ,
"Argentina" , "United Kingdom" , "France" , "Germany"
);
JSearchInput searchInput = new JSearchInput ();
searchInput . setPromptText ( "Search countries..." )
. setSuggestions (countries)
. setOnSearch (country -> {
System . out . println ( "Selected country: " + country);
});
return searchInput;
}
}
With Dynamic Suggestions
import com.jjarroyo.components.JSearchInput;
import java.util.List;
public class ProductSearch {
private ProductService productService ;
public JSearchInput createProductSearch () {
JSearchInput searchInput = new JSearchInput ();
searchInput . setPromptText ( "Search products..." )
. setSuggestionProvider (query -> {
// Fetch dynamic suggestions from API/database
if (query == null || query . isBlank ()) {
return List . of ();
}
return productService . searchProducts (query);
})
. setOnSearch (productName -> {
// Handle product selection
Product product = productService . findByName (productName);
showProductDetails (product);
});
return searchInput;
}
private void showProductDetails ( Product product ) {
// Display product details
}
}
Without Clear Button
JSearchInput searchInput = new JSearchInput ();
searchInput . setPromptText ( "Search..." )
. setClearable ( false )
. setOnSearch (query -> performSearch (query));
Behavior Details
Auto-hide : The popup automatically closes when focus is lost
Width : The popup matches the width of the search input
Max Height : Limited to 250px with vertical scrolling
Position : Appears 4px below the search input
Trigger : Opens when suggestions are available and text changes
Clear Button
Visibility : Shown only when text is present and clearable is true
Action : Clears the text, closes popup, and returns focus to the input
Text Changes
Text changes automatically trigger the suggestion provider
Empty or null suggestion lists hide the popup
Suggestions are filtered case-insensitively when using setSuggestions()
See Also