Skip to content

Commit 8403285

Browse files
committed
8268145: [macos] Rendering artifacts is seen when text inside the JTable with TableCellEditor having JTextfield
Reviewed-by: kizune, abhiscxk, honkar
1 parent aa38284 commit 8403285

File tree

2 files changed

+99
-8
lines changed

2 files changed

+99
-8
lines changed

src/java.desktop/macosx/classes/com/apple/laf/AquaCaret.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2024, 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
@@ -189,20 +189,25 @@ protected synchronized void damage(final Rectangle r) {
189189
// intersection of the caret rectangle and the component less the border, if any.
190190
final Rectangle caretRect = new Rectangle(x, y, width, height);
191191
final Border border = getComponent().getBorder();
192-
if (border != null) {
193-
final Rectangle alloc = getComponent().getBounds();
194-
alloc.x = alloc.y = 0;
192+
final Rectangle alloc = getComponent().getBounds();
193+
alloc.x = alloc.y = 0;
194+
if (border != null && border.isBorderOpaque()) {
195195
final Insets borderInsets = border.getBorderInsets(getComponent());
196196
alloc.x += borderInsets.left;
197197
alloc.y += borderInsets.top;
198198
alloc.width -= borderInsets.left + borderInsets.right;
199199
alloc.height -= borderInsets.top + borderInsets.bottom;
200200
Rectangle2D.intersect(caretRect, alloc, caretRect);
201+
x = caretRect.x;
202+
y = caretRect.y;
203+
width = Math.max(caretRect.width, 1);
204+
height = Math.max(caretRect.height, 1);
205+
} else {
206+
x = alloc.x;
207+
y = alloc.y;
208+
width = alloc.width;
209+
height = alloc.height;
201210
}
202-
x = caretRect.x;
203-
y = caretRect.y;
204-
width = Math.max(caretRect.width, 1);
205-
height = Math.max(caretRect.height, 1);
206211
repaint();
207212
}
208213

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8268145
27+
* @library /java/awt/regtesthelpers
28+
* @build PassFailJFrame
29+
* @requires (os.family == "mac")
30+
* @summary Verify rendering artifact is not seen moving caret inside
31+
* JTable with TableCellEditor having JTextField
32+
* @run main/manual TestCaretArtifact
33+
*/
34+
35+
import javax.swing.DefaultCellEditor;
36+
import javax.swing.JFrame;
37+
import javax.swing.JTable;
38+
import javax.swing.JTextField;
39+
import javax.swing.table.TableCellEditor;
40+
import javax.swing.table.TableColumn;
41+
import javax.swing.SwingUtilities;
42+
43+
public class TestCaretArtifact {
44+
45+
private static final String INSTRUCTIONS = """
46+
Double click on "Click Here" textfield so that textfield becomes editable;
47+
Press spacebar. Press left arrow button.
48+
Do this few times.
49+
If artifact is seen, press Fail else press Pass.""";
50+
51+
public static void main(String[] args) throws Exception {
52+
PassFailJFrame.builder()
53+
.title("Caret Artifact Instructions")
54+
.instructions(INSTRUCTIONS)
55+
.columns(35)
56+
.testUI(TestCaretArtifact::createUI)
57+
.build()
58+
.awaitAndCheck();
59+
}
60+
61+
62+
public static JFrame createUI() {
63+
TableCellEditor editor = new TestEditor(new JTextField());
64+
JTable table = new JTable(new Object[][] {{"click here",
65+
"inactive forever"}},
66+
new Object[] {"1", "2"});
67+
68+
JFrame frame = new JFrame("CaretArtifact");
69+
TableColumn column = table.getColumn("1");
70+
column.setCellEditor(editor);
71+
frame.getContentPane().add("Center", table);
72+
frame.setSize(400, 100);
73+
74+
return frame;
75+
}
76+
77+
static class TestEditor extends DefaultCellEditor {
78+
public TestEditor(JTextField tf) {
79+
super(tf);
80+
}
81+
public boolean stopCellEditing() {
82+
return false;
83+
}
84+
}
85+
}
86+

0 commit comments

Comments
 (0)