Skip to content

Commit 8ae2846

Browse files
committed
8232243: Wrong caret position in JTextPane on Windows with a screen resolution > 100%
Backport-of: ddb1d7a
1 parent a2af649 commit 8ae2846

File tree

3 files changed

+137
-2
lines changed

3 files changed

+137
-2
lines changed

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,7 @@ public int viewToModel(GlyphView v, float x, float y, Shape a,
176176
Segmenttext = v.getText(p0, p1);
177177
int[] justificationData = getJustificationData(v);
178178
intoffs = Utilities.getTabbedTextOffset(v, text, metrics,
179-
alloc.x, (int) x, expander, p0,
180-
justificationData);
179+
(float)alloc.x, x, expander, p0, justificationData);
181180
SegmentCache.releaseSharedSegment(text);
182181
intretValue = p0 + offs;
183182
if(retValue == p1) {

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

+8
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,14 @@ static final int getTabbedTextOffset(View view, Segment s, FontMetrics metrics,
398398
justificationData, false);
399399
}
400400

401+
staticfinalintgetTabbedTextOffset(Viewview, Segments, FontMetricsmetrics,
402+
floatx0, floatx, TabExpandere,
403+
intstartOffset,
404+
int[] justificationData) {
405+
returngetTabbedTextOffset(view, s, metrics, x0, x, e, startOffset, true,
406+
justificationData, true);
407+
}
408+
401409
/**
402410
* Determines the relative offset into the given text that
403411
* best represents the given span in the view coordinate
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Copyright (c) 2020, 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+
* @test
25+
* @bug 8232243
26+
* @key headful
27+
* @summary Verifies caret position in multiline JTextPane
28+
* in hidpi mode should be in sync with mouse press position.
29+
* @run main/othervm -Dsun.java2d.uiScale=2 TestCaretPositionJTextPane
30+
*/
31+
32+
importjavax.swing.JComboBox;
33+
importjavax.swing.JTextPane;
34+
importjavax.swing.JFrame;
35+
importjavax.swing.JScrollPane;
36+
importjavax.swing.SwingUtilities;
37+
importjavax.swing.text.BadLocationException;
38+
importjavax.swing.text.Caret;
39+
importjava.awt.Font;
40+
importjava.awt.BorderLayout;
41+
importjava.awt.Point;
42+
importjava.awt.Robot;
43+
importjava.awt.event.InputEvent;
44+
importjava.awt.event.MouseEvent;
45+
importjava.awt.event.MouseListener;
46+
importjava.awt.geom.Rectangle2D;
47+
48+
publicclassTestCaretPositionJTextPane {
49+
privatestaticJTextPanetextPane;
50+
privatestaticJFramef;
51+
52+
privatestaticvoidcreateUI() {
53+
f = newJFrame("Test Cursor/Caret with Java 9");
54+
55+
textPane = newJTextPane();
56+
textPane.setFont(newjava.awt.Font("Dialog", Font.PLAIN, 12));
57+
58+
fillTextPane(textPane);
59+
60+
textPane.addMouseListener(newMouseListener() {
61+
@Override
62+
publicvoidmouseClicked(MouseEvente) {}
63+
64+
@Override
65+
publicvoidmousePressed(MouseEvente) {
66+
try {
67+
Caretcaret = textPane.getCaret();
68+
Rectangle2Drect = textPane.modelToView2D(caret.getDot());
69+
70+
if (Math.abs(e.getPoint().x - rect.getX()) > 5) {
71+
System.out.println("mouse point " + e.getPoint());
72+
System.out.println("caret position " + rect);
73+
thrownewRuntimeException(" Wrong caret position");
74+
}
75+
} catch (BadLocationExceptionex) {}
76+
}
77+
78+
@Override
79+
publicvoidmouseReleased(MouseEvente) {}
80+
81+
@Override
82+
publicvoidmouseEntered(MouseEvente) {}
83+
84+
@Override
85+
publicvoidmouseExited(MouseEvente) {}
86+
});
87+
f.add(newJScrollPane(textPane), BorderLayout.CENTER);
88+
f.pack();
89+
f.setVisible(true);
90+
}
91+
92+
publicstaticvoidmain(Stringargs[]) throwsException {
93+
try {
94+
Robotrobot = newRobot();
95+
robot.setAutoDelay(200);
96+
97+
SwingUtilities.invokeAndWait(() -> createUI());
98+
99+
robot.waitForIdle();
100+
Pointp = textPane.getLocationOnScreen();
101+
robot.mouseMove(p.x+ 480, p.y+6);
102+
robot.waitForIdle();
103+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
104+
robot.waitForIdle();
105+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
106+
robot.waitForIdle();
107+
} finally {
108+
SwingUtilities.invokeAndWait(() -> f.dispose());
109+
}
110+
}
111+
112+
privatestaticvoidfillTextPane(JTextPanetextPane) {
113+
StringBuilderbuf = newStringBuilder();
114+
115+
for (inti = 0; i < 30; i++) {
116+
StringBuilderrow = newStringBuilder();
117+
for (intj = 0; j < 50; j++) {
118+
row.append(j);
119+
if (j % 5 == 0) {
120+
row.append(" ");
121+
}
122+
}
123+
buf.append(row).append(System.lineSeparator());
124+
}
125+
textPane.setText(buf.toString());
126+
textPane.setCaretPosition(0);
127+
}
128+
}

0 commit comments

Comments
 (0)
close