diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatClientProperties.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatClientProperties.java index ce55df19..4f398439 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatClientProperties.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatClientProperties.java @@ -914,6 +914,36 @@ public interface FlatClientProperties */ String TEXT_FIELD_SHOW_CLEAR_BUTTON = "JTextField.showClearButton"; + /** + * Specifies the callback that is invoked when a "clear" (or "cancel") button is clicked. + * If a callback is specified than it is responsible for clearing the text field. + * Without callback, the text field clears itself. + *
+ * Either use a {@link java.lang.Runnable}: + *
{@code
+ * myTextField.putClientProperty( "JTextField.clearCallback",
+ * (Runnable) () -> {
+ * // clear field here or cancel search
+ * } );
+ * }
+ * Or use a {@link java.util.function.Consumer}<javax.swing.text.JTextComponent>
+ * that receives the text field as parameter:
+ * {@code
+ * myTextField.putClientProperty( "JTextField.clearCallback",
+ * (Consumer) textField -> {
+ * // clear field here or cancel search
+ * } );
+ * }
+ *
+ * Component {@link javax.swing.JTextField} (and subclasses)
+ * Value type {@link java.lang.Runnable}
+ * or {@link java.util.function.Consumer}<javax.swing.text.JTextComponent>
+ *
+ * @see #TEXT_FIELD_SHOW_CLEAR_BUTTON
+ * @since 2
+ */
+ String TEXT_FIELD_CLEAR_CALLBACK = "JTextField.clearCallback";
+
//---- JToggleButton ------------------------------------------------------
/**
diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextFieldUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextFieldUI.java
index 62f9496e..0dca6fc8 100644
--- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextFieldUI.java
+++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextFieldUI.java
@@ -35,6 +35,7 @@ import java.beans.PropertyChangeEvent;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Consumer;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComboBox;
@@ -752,18 +753,6 @@ debug*/
}
}
- /** @since 2 */
- protected JComponent createClearButton() {
- JButton button = new JButton();
- button.putClientProperty( STYLE_CLASS, "clearButton" );
- button.putClientProperty( BUTTON_TYPE, BUTTON_TYPE_TOOLBAR_BUTTON );
- button.setCursor( Cursor.getDefaultCursor() );
- button.addActionListener( e -> {
- getComponent().setText( "" );
- } );
- return button;
- }
-
/** @since 2 */
protected void uninstallClearButton() {
if( clearButton != null ) {
@@ -772,6 +761,29 @@ debug*/
}
}
+ /** @since 2 */
+ protected JComponent createClearButton() {
+ JButton button = new JButton();
+ button.putClientProperty( STYLE_CLASS, "clearButton" );
+ button.putClientProperty( BUTTON_TYPE, BUTTON_TYPE_TOOLBAR_BUTTON );
+ button.setCursor( Cursor.getDefaultCursor() );
+ button.addActionListener( e -> clearButtonClicked() );
+ return button;
+ }
+
+ /** @since 2 */
+ @SuppressWarnings( "unchecked" )
+ protected void clearButtonClicked() {
+ JTextComponent c = getComponent();
+ Object callback = c.getClientProperty( TEXT_FIELD_CLEAR_CALLBACK );
+ if( callback instanceof Runnable )
+ ((Runnable)callback).run();
+ else if( callback instanceof Consumer )
+ ((Consumer