Skip to content

Conversation

@honkar-jdk
Copy link
Contributor

@honkar-jdk honkar-jdk commented Oct 22, 2025

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.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8353755: Add a helper method to Util - findComponent() (Enhancement - P4)

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/27944/head:pull/27944
$ git checkout pull/27944

Update a local copy of the PR:
$ git checkout pull/27944
$ git pull https://git.openjdk.org/jdk.git pull/27944/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 27944

View PR using the GUI difftool:
$ git pr show -t 27944

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/27944.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Oct 22, 2025

👋 Welcome back honkar! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Oct 22, 2025

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk openjdk bot added the client client-libs-dev@openjdk.org label Oct 22, 2025
@openjdk
Copy link

openjdk bot commented Oct 22, 2025

@honkar-jdk The following label will be automatically applied to this pull request:

  • client

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the rfr Pull request is ready for review label Oct 22, 2025
@mlbridge
Copy link

mlbridge bot commented Oct 22, 2025

Webrevs

Comment on lines +150 to +153
/**
* Find a component based on predicate.
* Always run this method on the EDT thread
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some tests where EDT calls are interleaved between calls to robot (non-EDT) (e.g bug4759934.java - findCancelButton()) and few other tests (e.g FileSizeCheck.java - findDetailsButton() and findTable() that are within upper level method which is called on EDT). Is it better to leave it as-is for flexibility rather than add a wrapper?

If a wrapper is added we might need to check if it is already on EDT thread or not as below:

if (isEventDispatchThread()) {
   return _findComponent(container, predicate);
} else {
  return Util.invokeOnEDT(() -> _findComponent(container, predicate));
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a wrapper is added we might need to check if it is already on EDT thread or not as below:

This is what I said: “the implementation will run directly or on EDT.” So if it's already on EDT, call directly; it it's not, use invokeOnEDT.

The utility method can then be used in all the contexts without thinking. (The implementation, if you choose this way, should rather be findComponentImpl rather than prefixed with an underscore.)

Likely, one still needs to call findComponent on EDT because any other operations on the component need to be on EDT. In this case, findComponent could throw an exception if it's called not on EDT.

These things will ensure the method can work correctly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +76 to +77
JButton cancel = Util.invokeOnEDT(() -> findCancelButton(jfc));
cancel.doClick();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doClick should also be called on EDT.

Comment on lines 231 to 236
private static AbstractButton findDetailsButton(final Container container) {
Component result = findComponent(container,
c -> c instanceof JToggleButton button
&& "Details".equals(button.getToolTipText()));
Component result = Util.findComponent(container,
c -> c instanceof JToggleButton button
&& "Details".equals(button.getToolTipText()));
return (AbstractButton) result;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, I would leave the wrapped predicate parameter as is; otherwise, the lines become too long. This is why I chose to use the 8-column indentation for the second parameter rather than aligning it with the opening parenthesis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

client client-libs-dev@openjdk.org rfr Pull request is ready for review

Development

Successfully merging this pull request may close these issues.

2 participants