Top Description Fields Constructors Methods
com.dlsc.gemsfx

public Class SearchTextField

extends CustomTextField
Class Inheritance
Imports
com.dlsc.gemsfx.util.HistoryManager, .StringHistoryManager, .UIUtil, javafx.beans.property.BooleanProperty, .ObjectProperty, .SimpleBooleanProperty, .SimpleObjectProperty, javafx.css.PseudoClass, javafx.event.ActionEvent, javafx.scene.Cursor, .Node, javafx.scene.control.Label, javafx.scene.input.KeyCode, .KeyEvent, javafx.scene.layout.Region, .StackPane, org.apache.commons.lang3.StringUtils, org.controlsfx.control.textfield.CustomTextField, org.kordamp.ikonli.javafx.FontIcon, org.kordamp.ikonli.materialdesign.MaterialDesign, java.util.Objects

A custom text field specifically designed for search functionality. This class enhances a text field with features such as a history of search terms, an optional history popup, and custom icons for search and clear operations.

The history manager is disabled by default, but it can be enabled using the setHistoryManager(HistoryManager) method. We have implemented a local history manager, StringHistoryManager, which uses the Java Preferences API to store history records. You can enable it via the setHistoryManager(HistoryManager) method.

By default, when the field loses its focus or the user presses the "enter" key (triggering the onAction event), the text is added to the history. This behavior can be disabled by setting the addingItemToHistoryOnEnterProperty() and / or the addingItemToHistoryOnEnterProperty() to false.
Additionally, history can be manually added based on user actions, such as after typing text and selecting an item from a ListView or TableView that displays results, or through other interactions, by calling the getHistoryManager() method to access the StringHistoryManager instance. then calling the StringHistoryManager#add(Object)} method.

Field Summary

Modifier and TypeField and Description
private BooleanProperty
private BooleanProperty
private static final boolean
private static final boolean
private static final boolean
private static final PseudoClass
private final HistoryButton<String>
private ObjectProperty<HistoryManager<String>>
private ObjectProperty<Node>
private BooleanProperty

Constructor Summary

AccessConstructor and Description
public
SearchTextField()

Constructs a new text field customized for search operations.

Method Summary

Modifier and TypeMethod and Description
private void
public final BooleanProperty

Returns:

true if the text should be added to the history on Enter, false otherwise
addingItemToHistoryOnEnterProperty
()

Determines whether the text of the text field should be added to the history when the user presses the Enter key.

public final BooleanProperty

Returns:

true if the text should be added to the history on focus lost, false otherwise
addingItemToHistoryOnFocusLostProperty
()

Determines whether the text of the text field should be added to the history when the field losses its focus.

private void
private HistoryButton<String>
private StackPane
public final HistoryManager<String>
public final Node
public String
getUserAgentStylesheet()

Overrides org.controlsfx.control.textfield.CustomTextField.getUserAgentStylesheet.

An implementation may specify its own user-agent styles for this Region, and its children, by overriding this method.

public final ObjectProperty<HistoryManager<String>>

Returns:

the property representing the history manager
historyManagerProperty
()

The history manager that is used to manage the history of the search text field.

public final ObjectProperty<Node>

Returns:

the property representing the history placeholder node
historyPlaceholderProperty
()

Returns the property representing the history placeholder node.

public final boolean
public final boolean
public final boolean
public final BooleanProperty

Returns:

true if the text field should have round corners, false otherwise
roundProperty
()

Determines whether the text field should have round corners.

public final void
setAddingItemToHistoryOnEnter(boolean addingItemToHistoryOnEnter)

public final void
setAddingItemToHistoryOnFocusLost(boolean addingItemToHistoryOnFocusLost)

public final void
public final void
setHistoryPlaceholder(Node historyPlaceholder)

public final void
setRound(boolean round)

Inherited from org.controlsfx.control.textfield.CustomTextField:
createDefaultSkingetLeftgetRightleftPropertyrightPropertysetLeftsetRight

Field Detail

addingItemToHistoryOnEnterback to summary
private BooleanProperty addingItemToHistoryOnEnter
addingItemToHistoryOnFocusLostback to summary
private BooleanProperty addingItemToHistoryOnFocusLost
DEFAULT_ADDING_ITEM_TO_HISTORY_ON_ENTERback to summary
private static final boolean DEFAULT_ADDING_ITEM_TO_HISTORY_ON_ENTER
DEFAULT_ADDING_ITEM_TO_HISTORY_ON_FOCUS_LOSTback to summary
private static final boolean DEFAULT_ADDING_ITEM_TO_HISTORY_ON_FOCUS_LOST
DEFAULT_ROUNDback to summary
private static final boolean DEFAULT_ROUND
DISABLED_POPUP_PSEUDO_CLASSback to summary
private static final PseudoClass DISABLED_POPUP_PSEUDO_CLASS
historyButtonback to summary
private final HistoryButton<String> historyButton
historyManagerback to summary
private ObjectProperty<HistoryManager<String>> historyManager
historyPlaceholderback to summary
private ObjectProperty<Node> historyPlaceholder
roundback to summary
private BooleanProperty round

Constructor Detail

SearchTextFieldback to summary
public SearchTextField()

Constructs a new text field customized for search operations.

The history manager is initialized with default values.

Method Detail

addEventHandlersback to summary
private void addEventHandlers()
addingItemToHistoryOnEnterPropertyback to summary
public final BooleanProperty addingItemToHistoryOnEnterProperty()

Determines whether the text of the text field should be added to the history when the user presses the Enter key.

Returns:BooleanProperty

true if the text should be added to the history on Enter, false otherwise

addingItemToHistoryOnFocusLostPropertyback to summary
public final BooleanProperty addingItemToHistoryOnFocusLostProperty()

Determines whether the text of the text field should be added to the history when the field losses its focus.

Returns:BooleanProperty

true if the text should be added to the history on focus lost, false otherwise

addToHistoryback to summary
private void addToHistory()
createHistoryButtonback to summary
private HistoryButton<String> createHistoryButton()
createRightNodeback to summary
private StackPane createRightNode()
getHistoryManagerback to summary
public final HistoryManager<String> getHistoryManager()
getHistoryPlaceholderback to summary
public final Node getHistoryPlaceholder()
getUserAgentStylesheetback to summary
public String getUserAgentStylesheet()

Overrides org.controlsfx.control.textfield.CustomTextField.getUserAgentStylesheet.

Doc from org.controlsfx.control.textfield.CustomTextField.getUserAgentStylesheet.

An implementation may specify its own user-agent styles for this Region, and its children, by overriding this method. These styles are used in addition to whatever user-agent stylesheets are in use. This provides a mechanism for third parties to introduce styles for custom controls.

The URL is a hierarchical URI of the form [scheme:][//authority][path]. If the URL does not have a [scheme:] component, the URL is considered to be the [path] component only. Any leading '/' character of the [path] is ignored and the [path] is treated as a path relative to the root of the application's classpath.

Subclasses overriding this method should not assume any particular implementation approach as to the number and frequency with which it is called. For this reason, attempting any kind of dynamic implementation (i.e. returning different user agent stylesheet values) based on some state change is highly discouraged, as there is no guarantee when, or even if, this method will be called. Some JavaFX CSS implementations may choose to cache this response for an indefinite period of time, and therefore there should be no expectation around when this method is called.



package com.example.javafx.app;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class MyApp extends Application {

    @Override public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        scene.getStylesheets().add("/com/example/javafx/app/mystyles.css");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
For additional information about using CSS with the scene graph, see the CSS Reference Guide.
Annotations
@Override
historyManagerPropertyback to summary
public final ObjectProperty<HistoryManager<String>> historyManagerProperty()

The history manager that is used to manage the history of the search text field.

If its value is null, the history feature will not be enabled, which means only the magnifying glass icon will be displayed, and the dropdown arrow next to the magnifying glass will not be shown.

If its value is not null, the history feature will be enabled, meaning that both the magnifying glass icon and the dropdown arrow will be displayed. Clicking the magnifying glass icon button will display the history popup.

To enable the history feature, you need to set an instance of HistoryManager. Typically, you would use an instance of StringHistoryManager, which is an implementation of HistoryManager that manages string-type history records.

Returns:ObjectProperty<HistoryManager<String>>

the property representing the history manager

historyPlaceholderPropertyback to summary
public final ObjectProperty<Node> historyPlaceholderProperty()

Returns the property representing the history placeholder node.

Returns:ObjectProperty<Node>

the property representing the history placeholder node

isAddingItemToHistoryOnEnterback to summary
public final boolean isAddingItemToHistoryOnEnter()
isAddingItemToHistoryOnFocusLostback to summary
public final boolean isAddingItemToHistoryOnFocusLost()
isRoundback to summary
public final boolean isRound()
roundPropertyback to summary
public final BooleanProperty roundProperty()

Determines whether the text field should have round corners.

Returns:BooleanProperty

true if the text field should have round corners, false otherwise

setAddingItemToHistoryOnEnterback to summary
public final void setAddingItemToHistoryOnEnter(boolean addingItemToHistoryOnEnter)
setAddingItemToHistoryOnFocusLostback to summary
public final void setAddingItemToHistoryOnFocusLost(boolean addingItemToHistoryOnFocusLost)
setHistoryManagerback to summary
public final void setHistoryManager(HistoryManager<String> historyManager)
setHistoryPlaceholderback to summary
public final void setHistoryPlaceholder(Node historyPlaceholder)
setRoundback to summary
public final void setRound(boolean round)