Skip to content

Commit 246f48e

Browse files
committed
8212904: JTextArea line wrapping incorrect when using UI scale
Backport-of: 702ee3d
1 parent 684f12e commit 246f48e

File tree

3 files changed

+119
-2
lines changed

3 files changed

+119
-2
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ static final int getBreakLocation(Segment s, FontMetrics metrics,
665665
public static final int getBreakLocation(Segment s, FontMetrics metrics,
666666
float x0, float x, TabExpander e,
667667
int startOffset) {
668-
return getBreakLocation(s, metrics, x0, x, e, startOffset, false);
668+
return getBreakLocation(s, metrics, x0, x, e, startOffset, true);
669669
}
670670

671671
/**

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,8 @@ protected int calculateBreakPosition(int p0, int p1) {
360360
int currentWidth = getWidth();
361361
if (wordWrap) {
362362
p = p0 + Utilities.getBreakLocation(segment, metrics,
363-
tabBase, tabBase + currentWidth,
363+
(float)tabBase,
364+
(float)(tabBase + currentWidth),
364365
this, p0);
365366
} else {
366367
p = p0 + Utilities.getTabbedTextOffset(segment, metrics,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright (c) 2019, 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+
* @key headful
26+
* @bug 8212904
27+
* @summary Verifies JTextArea line wrapping using UI scale
28+
* @run main JTextAreaWordWrapTest
29+
*/
30+
import java.awt.BorderLayout;
31+
import java.awt.event.WindowAdapter;
32+
import java.awt.event.WindowEvent;
33+
import java.io.IOException;
34+
import java.net.URISyntaxException;
35+
import javax.swing.JFrame;
36+
import javax.swing.JScrollPane;
37+
import javax.swing.JTextArea;
38+
import javax.swing.ScrollPaneConstants;
39+
import javax.swing.SwingUtilities;
40+
41+
public class JTextAreaWordWrapTest {
42+
43+
static JFrame frame;
44+
static JFrame frame1;
45+
static JTextArea textArea;
46+
static JTextArea textArea1;
47+
48+
public static void doWrapOnTest() {
49+
50+
frame = new JFrame();
51+
frame.setSize( 720, 300 );
52+
frame.setLayout( new BorderLayout() );
53+
54+
textArea = new JTextArea();
55+
textArea.setLineWrap( true );
56+
textArea.setWrapStyleWord( true );
57+
58+
StringBuffer sb = new StringBuffer();
59+
for (int i = 0; i < 100; i++) {
60+
sb.append( "zz zzz zzzz zz zz zz zzz xzzzz zzzzzzzzzzzzzzzzx yyyyyyy tttttttttt sssss hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\n" );
61+
}
62+
textArea.setText( sb.toString() );
63+
JScrollPane pane = new JScrollPane( textArea,
64+
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
65+
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED );
66+
frame.add( pane, BorderLayout.CENTER );
67+
frame.setVisible( true );
68+
69+
}
70+
71+
public static void doWrapOffTest() {
72+
frame1 = new JFrame();
73+
frame1.setSize( 720, 300 );
74+
frame1.setLayout( new BorderLayout() );
75+
76+
textArea1 = new JTextArea();
77+
78+
StringBuffer sb1 = new StringBuffer();
79+
for (int i = 0; i < 100; i++) {
80+
sb1.append( "zz zzz zzzz zz zz zz zzz xzzzz zzzzzzzzzzzzzzzzx yyyyyyy tttttttttt sssss hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\n" );
81+
}
82+
textArea1.setText( sb1.toString() );
83+
JScrollPane pane1 = new JScrollPane( textArea1,
84+
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
85+
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED );
86+
frame1.add( pane1, BorderLayout.CENTER );
87+
frame1.setLocationRelativeTo(null);
88+
frame1.setVisible( true );
89+
}
90+
91+
public static void main( String[] args ) throws Exception {
92+
System.setProperty( "sun.java2d.uiScale", "1.25" );
93+
try {
94+
SwingUtilities.invokeAndWait(() -> doWrapOnTest());
95+
Thread.sleep(500);
96+
SwingUtilities.invokeAndWait(() -> doWrapOffTest());
97+
Thread.sleep(500);
98+
99+
SwingUtilities.invokeAndWait(() -> {
100+
101+
int wraponHeight = textArea.getHeight();
102+
System.out.println("wraponheight " + wraponHeight);
103+
int wrapoffHeight = textArea1.getHeight();
104+
System.out.println("wrapoffheight " + wrapoffHeight);
105+
106+
if (wraponHeight == wrapoffHeight) {
107+
throw new RuntimeException("JTextArea line wrapping incorrect when using UI scale");
108+
}
109+
});
110+
111+
} finally {
112+
SwingUtilities.invokeAndWait(() -> frame.dispose());
113+
SwingUtilities.invokeAndWait(() -> frame1.dispose());
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)