Skip to content

Commit 635c4b9

Browse files
author
Alexander Zuev
committed
8377727: Ghost caret and focus appear in non‑editable text fields
Reviewed-by: prr, dmarkov, aivanov
1 parent 2fe7ae9 commit 635c4b9

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

src/java.desktop/share/classes/javax/swing/text/DefaultCaret.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,6 +25,7 @@
2525

2626
package javax.swing.text;
2727

28+
import java.awt.Color;
2829
import java.awt.Graphics;
2930
import java.awt.HeadlessException;
3031
import java.awt.Point;
@@ -690,7 +691,25 @@ public void paint(Graphics g) {
690691
// semantics of damage we can't really get around this.
691692
damage(r);
692693
}
693-
g.setColor(component.getCaretColor());
694+
if (component.isEditable()) {
695+
g.setColor(component.getCaretColor());
696+
} else {
697+
Color caretColor = component.getCaretColor();
698+
if (caretColor == null) {
699+
caretColor = g.getColor();
700+
}
701+
Color bg = component.getBackground();
702+
if (bg == null) {
703+
g.setColor(caretColor);
704+
} else {
705+
int red = (caretColor.getRed() + bg.getRed()) / 2;
706+
int green = (caretColor.getGreen() + bg.getGreen()) / 2;
707+
int blue = (caretColor.getBlue() + bg.getBlue()) / 2;
708+
int alpha = 127;
709+
Color newCaretColor = new Color(red, green, blue, alpha);
710+
g.setColor(newCaretColor);
711+
}
712+
}
694713
int paintWidth = getCaretWidth(r.height);
695714
r.x -= paintWidth >> 1;
696715
g.fillRect(r.x, r.y, paintWidth, r.height);

0 commit comments

Comments
 (0)