From 941441d7e1d5977fe90ee7747faef3345b71b9f1 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Wed, 5 Jan 2022 17:23:28 +0100 Subject: [PATCH] TextField: clear button has now component name `TextField.clearButton` PasswordField: reveals button has now component name `PasswordField.revealButton` and additional style class `revealButton` (issue #173) SwingUtils: added `getComponentByName()` for easy getting clear or reveal buttons --- .../formdev/flatlaf/ui/FlatOptionPaneUI.java | 17 +------ .../flatlaf/ui/FlatPasswordFieldUI.java | 2 + .../formdev/flatlaf/ui/FlatTextFieldUI.java | 2 + .../com/formdev/flatlaf/util/SwingUtils.java | 50 +++++++++++++++++++ 4 files changed, 56 insertions(+), 15 deletions(-) create mode 100644 flatlaf-core/src/main/java/com/formdev/flatlaf/util/SwingUtils.java diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatOptionPaneUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatOptionPaneUI.java index 46e8294a..e1c8e69a 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatOptionPaneUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatOptionPaneUI.java @@ -36,6 +36,7 @@ import javax.swing.plaf.UIResource; import javax.swing.plaf.basic.BasicHTML; import javax.swing.plaf.basic.BasicOptionPaneUI; import com.formdev.flatlaf.FlatClientProperties; +import com.formdev.flatlaf.util.SwingUtils; import com.formdev.flatlaf.util.UIScale; /** @@ -156,7 +157,7 @@ public class FlatOptionPaneUI // set icon-message gap if( iconMessageGap > 0 ) { - Component iconMessageSeparator = findByName( messageArea, "OptionPane.separator" ); + Component iconMessageSeparator = SwingUtils.getComponentByName( messageArea, "OptionPane.separator" ); if( iconMessageSeparator != null ) iconMessageSeparator.setPreferredSize( new Dimension( UIScale.scale( iconMessageGap ), 1 ) ); } @@ -236,20 +237,6 @@ public class FlatOptionPaneUI } } - private Component findByName( Container c, String name ) { - for( Component child : c.getComponents() ) { - if( name.equals( child.getName() ) ) - return child; - - if( child instanceof Container ) { - Component c2 = findByName( (Container) child, name ); - if( c2 != null ) - return c2; - } - } - return null; - } - @Override protected boolean getSizeButtonsToSameWidth() { return sameSizeButtons; diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPasswordFieldUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPasswordFieldUI.java index 1461db2f..4e7c7679 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPasswordFieldUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPasswordFieldUI.java @@ -291,7 +291,9 @@ public class FlatPasswordFieldUI /** @since 2 */ protected JToggleButton createRevealButton() { JToggleButton button = new JToggleButton( revealIcon ); + button.setName( "PasswordField.revealButton" ); prepareLeadingOrTrailingComponent( button ); + button.putClientProperty( FlatClientProperties.STYLE_CLASS, "inTextField revealButton" ); if( FlatClientProperties.clientPropertyBoolean( getComponent(), KEY_REVEAL_SELECTED, false ) ) { button.setSelected( true ); updateEchoChar( true ); 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 0dca6fc8..66c7056d 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 @@ -764,6 +764,7 @@ debug*/ /** @since 2 */ protected JComponent createClearButton() { JButton button = new JButton(); + button.setName( "TextField.clearButton" ); button.putClientProperty( STYLE_CLASS, "clearButton" ); button.putClientProperty( BUTTON_TYPE, BUTTON_TYPE_TOOLBAR_BUTTON ); button.setCursor( Cursor.getDefaultCursor() ); @@ -937,6 +938,7 @@ debug*/ ((LayoutManager2)delegate).invalidateLayout( target ); } } + //---- class FlatDocumentListener ----------------------------------------- private class FlatDocumentListener diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/SwingUtils.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/SwingUtils.java new file mode 100644 index 00000000..5fd8e785 --- /dev/null +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/SwingUtils.java @@ -0,0 +1,50 @@ +/* + * Copyright 2022 FormDev Software GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.formdev.flatlaf.util; + +import java.awt.Component; +import java.awt.Container; + +/** + * Utility methods for Swing. + * + * @author Karl Tauber + * @since 2 + */ +public class SwingUtils +{ + /** + * Search for a (grand) child component with the given name. + * + * @return a component; or {@code null} + */ + @SuppressWarnings( "unchecked" ) + public static T getComponentByName( Container parent, String name ) { + for( Component child : parent.getComponents() ) { + if( name.equals( child.getName() ) ) + return (T) child; + + if( child instanceof Container ) { + T c = getComponentByName( (Container) child, name ); + if( c != null ) + return c; + } + } + + return null; + } +}