Skip to content

Conversation

@anass-baya
Copy link
Contributor

@anass-baya anass-baya commented Dec 1, 2025

Analysis :
The issue is that on Win32, if the window is not active, we dont receive mouse events because we release the capture
So the problem is the following:
We start selecting text from the right to the extreme left.
Then we switch to another window( the window lose focus ), and we release the mouse
But when we return to the window, the caret drag is still active and does not stop. as the window did not recieved the mouse release event

Proposed fix:
In the Caret class, we added a logic to ignore the drag if the focus was lost due to window switching unless a new mouse press happens


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-8319880: JTextField text selection doesn't stop if ended during loss of window focus (Bug - P3)

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 28582

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@anass-baya anass-baya marked this pull request as draft December 1, 2025 16:08
@bridgekeeper
Copy link

bridgekeeper bot commented Dec 1, 2025

👋 Welcome back abaya! 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 Dec 1, 2025

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

@openjdk
Copy link

openjdk bot commented Dec 1, 2025

@anass-baya 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.

@anass-baya anass-baya changed the title Draft: 8319880: JTextField text selection doesn't stop if ended during loss of window focus 8319880: JTextField text selection doesn't stop if ended during loss of window focus Dec 1, 2025
@anass-baya anass-baya marked this pull request as ready for review December 1, 2025 16:27
@openjdk openjdk bot added the rfr Pull request is ready for review label Dec 1, 2025
@mlbridge
Copy link

mlbridge bot commented Dec 1, 2025

Webrevs

@anass-baya anass-baya marked this pull request as draft December 1, 2025 17:37
@openjdk openjdk bot removed the rfr Pull request is ready for review label Dec 1, 2025
@anass-baya anass-baya marked this pull request as ready for review December 3, 2025 10:38
@openjdk openjdk bot added the rfr Pull request is ready for review label Dec 3, 2025
@anass-baya anass-baya marked this pull request as draft December 3, 2025 10:45
@anass-baya anass-baya marked this pull request as ready for review December 3, 2025 10:48
@openjdk openjdk bot added rfr Pull request is ready for review and removed rfr Pull request is ready for review labels Dec 3, 2025

robot.delay(500);

Point location = textField.getLocationOnScreen();
Copy link
Member

Choose a reason for hiding this comment

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

These calls are still need to happen on EDT.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OKay, thanks

@@ -539,6 +547,8 @@ public void mousePressed(MouseEvent e) {
int nclicks = SwingUtilities2.getAdjustedClickCount(getComponent(), e);

if (SwingUtilities.isLeftMouseButton(e)) {
lastClickMillis = e.getWhen();
dragActive = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

should we set dragActive here? it's just mousePressed, in which case all mousePress event will have dragActive set.
I guess it should be set in mouseDragged and reset in mouseReleased, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The issue is that when we start selecting text and then switch to another window, our window loses focus. After losing focus, we no longer receive mouse events. So when we release the mouse button in the other window and then switch back, our window thinks the drag is still happening because it never received the mouse-release event.

To fix this, we added logic to stop the drag as soon as we lose focus. And when we regain focus, since we don’t know whether the mouse was released or not, we require a new mouse-press before allowing dragging again. We also added logic to ignore outdated drag events: any drag event that happened before the last mouse-press is considered outdated and is ignored.

So now the behavior matches what we see in native apps like Notepad. @aivanov-jdk asked to check how a native Windows app behaves in this situation and to make our behavior consistent with that.

This logic does not affect the behavior on macOS.

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

Labels

client [email protected] rfr Pull request is ready for review

Development

Successfully merging this pull request may close these issues.

3 participants