Skip to content

8377727: Ghost caret and focus appear in non‑editable text fields#30758

Closed
azuev-java wants to merge 2 commits intoopenjdk:masterfrom
azuev-java:JDK-8377727
Closed

8377727: Ghost caret and focus appear in non‑editable text fields#30758
azuev-java wants to merge 2 commits intoopenjdk:masterfrom
azuev-java:JDK-8377727

Conversation

@azuev-java
Copy link
Copy Markdown
Member

@azuev-java azuev-java commented Apr 15, 2026

Make caret color closer to the background color so it does appear as disabled caret.

Here is the comparison of default caret and the caret after the fix is applied:
Default white background: white_editable
After fix white background: white_noneditable
Default red background: red_editable
After fix with red background: red_noneditable



Progress

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

Issue

  • JDK-8377727: Ghost caret and focus appear in non‑editable text fields (Bug - P3)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 30758

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

Using diff file

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

Using Webrev

Link to Webrev Comment

…JDK 8u451+

Make caret color closer to the background color so it does appear as disabled caret.
@bridgekeeper
Copy link
Copy Markdown

bridgekeeper Bot commented Apr 15, 2026

👋 Welcome back kizune! 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
Copy Markdown

openjdk Bot commented Apr 15, 2026

@azuev-java This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8377727: Ghost caret and focus appear in non‑editable text fields

Reviewed-by: prr, dmarkov, aivanov

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 240 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk Bot added the client client-libs-dev@openjdk.org label Apr 15, 2026
@openjdk
Copy link
Copy Markdown

openjdk Bot commented Apr 15, 2026

@azuev-java 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 Apr 15, 2026
@mlbridge
Copy link
Copy Markdown

mlbridge Bot commented Apr 15, 2026

Webrevs

@azuev-java azuev-java changed the title 8377727: Ghost caret and focus appear in non‑editable text fields on JDK 8u451+ 8377727: Ghost caret and focus appear in non‑editable text fields Apr 15, 2026
@openjdk
Copy link
Copy Markdown

openjdk Bot commented Apr 16, 2026

The total number of required reviews for this PR has been set to 2 based on the presence of this label: client. This can be overridden with the /reviewers command.

Copy link
Copy Markdown
Contributor

@prrace prrace left a comment

Choose a reason for hiding this comment

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

OK. I'm sure there'll be some opinions about whether it is now too low-contrast, but it does need to be distinguishable from the normal caret, otherwise people may think we didn't change anything.

@openjdk openjdk Bot added the ready Pull request is ready to be integrated label Apr 21, 2026
int red = (caretColor.getRed() + bg.getRed()) / 2;
int green = (caretColor.getGreen() + bg.getGreen()) / 2;
int blue = (caretColor.getBlue() + bg.getBlue()) / 2;
int alpha = 127;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would it be enough to change the alpha component of the caret only?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That might be simpler.
It occurred that the code above could cause an IAE since the Color constructor requires values to be in range.
If the caret is white and the bg is say rgb(2, 2,2) then you'll pass 256 and get an exception.
It needs to be clamped.

There are also methods on java.awt.Color
Color.darker()
and
Color.brighter()

which could be used, but you'd first need to examine the caret color and probably the background too, to know which one is appropriate.

ie for a light bg with a dark caret, you'll want to use brighter() to reduce the contrast.
And vice versa.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

If the caret is white and the bg is say rgb(2, 2,2) then you'll pass 256

No, i will not. The white is rgb(255, 255, 255) and bg is rgb(2, 2, 2) then i will pass rgb ((2 + 255) / 2, (2 + 255) / 2, (2 + 255) /2) which is rgb(128, 128, 128) which is gray - perfectly fits the purpose of the fix.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Would it be enough to change the alpha component of the caret only?

Not sure it will work good on all color combinations so i did both added alpha value of 50% and brought color half way to the background.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If the caret is white and the bg is say rgb(2, 2,2) then you'll pass 256

No, i will not. The white is rgb(255, 255, 255) and bg is rgb(2, 2, 2) then i will pass rgb ((2 + 255) / 2, (2 + 255) / 2, (2 + 255) /2) which is rgb(128, 128, 128) which is gray - perfectly fits the purpose of the fix.

You're right. I mis-read the parens.

@azvegint
Copy link
Copy Markdown
Member

azvegint commented Apr 21, 2026

I see an NPE when I focus a disabled text field with the javax.swing.plaf.nimbus.NimbusLookAndFeel LaF enabled.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "java.awt.Color.getRed()" because "caretColor" is null
        at java.desktop/javax.swing.text.DefaultCaret.paint(DefaultCaret.java:699)
        at java.desktop/javax.swing.plaf.basic.BasicTextUI.paintSafely(BasicTextUI.java:774)
        at java.desktop/javax.swing.plaf.basic.BasicTextUI.paint(BasicTextUI.java:927)
        at java.desktop/javax.swing.plaf.synth.SynthTextFieldUI.paint(SynthTextFieldUI.java:201)
        at java.desktop/javax.swing.plaf.synth.SynthTextFieldUI.update(SynthTextFieldUI.java:185)
        at java.desktop/javax.swing.JComponent.paintComponent(JComponent.java:838)
        at java.desktop/javax.swing.JComponent.paint(JComponent.java:1114)
        at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:947)
        at java.desktop/javax.swing.JComponent.paint(JComponent.java:1123)
        at java.desktop/javax.swing.JComponent.paintToOffscreen(JComponent.java:5283)
        at java.desktop/javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:255)
        at java.desktop/javax.swing.RepaintManager.paint(RepaintManager.java:1240)
        at java.desktop/javax.swing.JComponent._paintImmediately(JComponent.java:5231)
        at java.desktop/javax.swing.JComponent.paintImmediately(JComponent.java:5041)
        at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:786)
        at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:749)
        at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:699)
        at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1793)
        at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:323)
        at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:714)
        at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
        at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)     

@prrace
Copy link
Copy Markdown
Contributor

prrace commented Apr 21, 2026

I see an NPE when I focus a disabled text field with the javax.swing.plaf.nimbus.NimbusLookAndFeel LaF enabled.

hmm. That's clearly a problem. And surprising. The existing code doesn't have any obvious protection against null.

@prrace
Copy link
Copy Markdown
Contributor

prrace commented Apr 21, 2026

I see an NPE when I focus a disabled text field with the javax.swing.plaf.nimbus.NimbusLookAndFeel LaF enabled.

hmm. That's clearly a problem. And surprising. The existing code doesn't have any obvious protection against null.
OTOH, it doesn't "use" it - just passes it to setColor() - which just ignores null (!)

I guess this means that the current graphics color is the fall back .. so if there's no caret color you need to use the current graphics color instead.
Then it is for another day as to whether that should really be null.

@azuev-java
Copy link
Copy Markdown
Member Author

I guess this means that the current graphics color is the fall back .. so if there's no caret color you need to use the current graphics color instead. Then it is for another day as to whether that should really be null.

Adding protection from null caret color and null background color (just in case).

@openjdk openjdk Bot removed the ready Pull request is ready to be integrated label Apr 21, 2026
Copy link
Copy Markdown
Contributor

@prrace prrace left a comment

Choose a reason for hiding this comment

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

need to avoid the IAE

@azuev-java
Copy link
Copy Markdown
Member Author

need to avoid the IAE

Which happens how?

Comment on lines +702 to +703
if (bg == null) {
g.setColor(caretColor);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does it make sense to add alpha to the caret if background color is null?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is a fallback which in theory should never happen because according to documentation it is impossible to set caret color to null, documentation for setCaretColor says:
Setting to null effectively restores the default color.
so i would leave it as is and instead looked at the Nimbus LaF to see why does it return null caret color for the disabled caret. Using the foreground color is a workaround to repeat the current behavior, not a proper solution. It might backfire on custom text component where user paints with some weird color something between text draw and caret painting.

@openjdk openjdk Bot added the ready Pull request is ready to be integrated label Apr 22, 2026
@azuev-java
Copy link
Copy Markdown
Member Author

/integrate

@openjdk
Copy link
Copy Markdown

openjdk Bot commented Apr 23, 2026

Going to push as commit 635c4b9.
Since your change was applied there have been 247 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk Bot added the integrated Pull request has been integrated label Apr 23, 2026
@openjdk openjdk Bot closed this Apr 23, 2026
@openjdk openjdk Bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Apr 23, 2026
@openjdk
Copy link
Copy Markdown

openjdk Bot commented Apr 23, 2026

@azuev-java Pushed as commit 635c4b9.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@azuev-java
Copy link
Copy Markdown
Member Author

/backport jdk26u

@azuev-java azuev-java deleted the JDK-8377727 branch April 24, 2026 23:35
@openjdk
Copy link
Copy Markdown

openjdk Bot commented Apr 24, 2026

@azuev-java the backport was successfully created on the branch backport-azuev-java-635c4b96-master in my personal fork of openjdk/jdk26u. To create a pull request with this backport targeting openjdk/jdk26u:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 635c4b96 from the openjdk/jdk repository.

The commit being backported was authored by Alexander Zuev on 23 Apr 2026 and was reviewed by Phil Race, Dmitry Markov and Alexey Ivanov.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk26u:

$ git fetch https://github.com/openjdk-bots/jdk26u.git backport-azuev-java-635c4b96-master:backport-azuev-java-635c4b96-master
$ git checkout backport-azuev-java-635c4b96-master
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk26u.git backport-azuev-java-635c4b96-master

⚠️ @azuev-java You are not yet a collaborator in my fork openjdk-bots/jdk26u. An invite will be sent out and you need to accept it before you can proceed.

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 integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.

5 participants