mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2025-12-09 16:25:10 +03:00
TextField: added "clear" callback
This commit is contained in:
@@ -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.
|
||||
* <p>
|
||||
* Either use a {@link java.lang.Runnable}:
|
||||
* <pre>{@code
|
||||
* myTextField.putClientProperty( "JTextField.clearCallback",
|
||||
* (Runnable) () -> {
|
||||
* // clear field here or cancel search
|
||||
* } );
|
||||
* }</pre>
|
||||
* Or use a {@link java.util.function.Consumer}<javax.swing.text.JTextComponent>
|
||||
* that receives the text field as parameter:
|
||||
* <pre>{@code
|
||||
* myTextField.putClientProperty( "JTextField.clearCallback",
|
||||
* (Consumer<JTextComponent>) textField -> {
|
||||
* // clear field here or cancel search
|
||||
* } );
|
||||
* }</pre>
|
||||
* <p>
|
||||
* <strong>Component</strong> {@link javax.swing.JTextField} (and subclasses)<br>
|
||||
* <strong>Value type</strong> {@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 ------------------------------------------------------
|
||||
|
||||
/**
|
||||
|
||||
@@ -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<JTextComponent>)callback).accept( c );
|
||||
else
|
||||
c.setText( "" );
|
||||
}
|
||||
|
||||
/** @since 2 */
|
||||
protected void updateClearButton() {
|
||||
if( clearButton == null )
|
||||
|
||||
Reference in New Issue
Block a user