RFR: JDK-8353755 : Add a helper method to Util - findComponent()

Alexey Ivanov aivanov at openjdk.org
Thu Oct 23 14:01:55 UTC 2025


On Wed, 22 Oct 2025 22:02:42 GMT, Harshitha Onkar <honkar at openjdk.org> wrote:

> `findComponent(final Container container, final Predicate<Component> predicate)` is a useful utility method and thus added to` javax/swing/regtesthelpers/Util.java` instead of having redundant code in tests. It can be used to find a component by label name.
> 
> PS: Existing `Util.findSubComponent()` finds component by class name but `findComponent()` can be used to search for a particular component by label name/title when there are multiple subcomponents of same type by applying a predicate logic.

Changes requested by aivanov (Reviewer).

test/jdk/javax/swing/JFileChooser/FileSizeCheck.java line 240:

> 238:     private static JTable findTable(final Container container) {
> 239:         Component result = Util.findComponent(container,
> 240:                                          c -> c instanceof JTable);

Suggestion:

        Component result = Util.findComponent(container,
                                              c -> c instanceof JTable);

I'm sure the second parameter was aligned to the opening parenthesis, it needs to be indented after you added 5 chars to the method call.

test/jdk/javax/swing/JFileChooser/bug4759934.java line 81:

> 79:             }
> 80:             Point cancelLoc = Util.getCenterPoint(cancel);
> 81:             robot.mouseMove(cancelLoc.x, cancelLoc.y);

I am not sure this is the way to go… However, [JDK-4759934](https://bugs.openjdk.org/browse/JDK-4759934) doesn't seem to depend on how the dialog is closed by pressing <kbd>Esc</kbd> or by clicking the **Cancel** button. If it's the case, click the button programmatically by calling `cancel.doClick()`, it's even less error-prone than clicking the button with a robot by sending mouse click.

test/jdk/javax/swing/JFileChooser/bug4759934.java line 133:

> 131:         Component result = Util.findComponent(container,
> 132:                                          c -> c instanceof JButton button
> 133:                                               && "Cancel".equals(button.getText()));

Suggestion:

        Component result = Util.findComponent(container,
                                              c -> c instanceof JButton button
                                                   && "Cancel".equals(button.getText()));

Aligning.

test/jdk/javax/swing/regtesthelpers/Util.java line 1:

> 1: /*

> Existing `Util.findSubComponent()` finds component by class name

To eliminate code duplication, the existing `findSubComponent` should use the new `findComponent` which is more flexible. Just pass `parent.getClass().getName().contains(className)` as the predicate to the new method.

test/jdk/javax/swing/regtesthelpers/Util.java line 153:

> 151:      * Find a component based on predicate.
> 152:      * Always run this method on the EDT thread
> 153:      */

The documentation could then explain the parameters, don't you think?

> Always run this method on the EDT thread

The utility method could ensure this automatically. This *public* method will be a wrapper around a *private* implementation, the implementation will run directly or on EDT using `invokeOnEDT` which already exists in the `Util` class.

test/jdk/javax/swing/regtesthelpers/Util.java line 155:

> 153:      */
> 154:     public static Component findComponent(final Container container,
> 155:                                            final Predicate<Component> predicate) {

Suggestion:

    public static Component findComponent(final Container container,
                                          final Predicate<Component> predicate) {

-------------

PR Review: https://git.openjdk.org/jdk/pull/27944#pullrequestreview-3370023732
PR Review Comment: https://git.openjdk.org/jdk/pull/27944#discussion_r2455199133
PR Review Comment: https://git.openjdk.org/jdk/pull/27944#discussion_r2455231458
PR Review Comment: https://git.openjdk.org/jdk/pull/27944#discussion_r2455208680
PR Review Comment: https://git.openjdk.org/jdk/pull/27944#discussion_r2455242012
PR Review Comment: https://git.openjdk.org/jdk/pull/27944#discussion_r2455187844
PR Review Comment: https://git.openjdk.org/jdk/pull/27944#discussion_r2455188360


More information about the client-libs-dev mailing list