Skip to content

Commit 99ff268

Browse files
committed
8239334: Tab Size does not work correctly in JTextArea with setLineWrap on
Backport-of: 3c72042
1 parent e2f8301 commit 99ff268

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ final void updateMetrics() {
465465
public float nextTabStop(float x, int tabOffset) {
466466
if (tabSize == 0)
467467
return x;
468-
float ntabs = (x - tabBase) / tabSize;
468+
int ntabs = (int) ((x - tabBase) / tabSize);
469469
return tabBase + ((ntabs + 1) * tabSize);
470470
}
471471

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
/**
25+
* @test
26+
* @bug 8239334
27+
* @summary Verifies Tab Size works correctly in JTextArea with setLineWrap on
28+
* @key headful
29+
* @run main TestTabSizeWithLineWrap
30+
*/
31+
32+
import java.awt.geom.Rectangle2D;
33+
import javax.swing.GroupLayout;
34+
import javax.swing.JFrame;
35+
import javax.swing.JScrollPane;
36+
import javax.swing.JTextArea;
37+
import javax.swing.SwingUtilities;
38+
import javax.swing.text.BadLocationException;
39+
import javax.swing.text.Caret;
40+
41+
public class TestTabSizeWithLineWrap {
42+
private static JScrollPane jScrollPane1;
43+
private static JTextArea jTextArea1;
44+
private static JFrame f;
45+
private static Rectangle2D rect;
46+
private static Rectangle2D rect1;
47+
private static boolean excpnthrown = false;
48+
49+
public static void main(String args[]) throws Exception {
50+
51+
SwingUtilities.invokeAndWait(() -> {
52+
try {
53+
jScrollPane1 = new JScrollPane();
54+
jTextArea1 = new JTextArea();
55+
jTextArea1.setTabSize(8);
56+
f = new JFrame();
57+
58+
jTextArea1.setFont(new java.awt.Font("Monospaced", 0, 10));
59+
jTextArea1.setLineWrap( true );
60+
String str =
61+
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#\n"
62+
+ "! Some Text\t\t\t\t\t#";
63+
jTextArea1.setText(str);
64+
jScrollPane1.setViewportView(jTextArea1);
65+
66+
GroupLayout layout = new javax.swing.GroupLayout(f.getContentPane());
67+
f.getContentPane().setLayout(layout);
68+
layout.setHorizontalGroup(
69+
layout.createParallelGroup(
70+
javax.swing.GroupLayout.Alignment.LEADING)
71+
.addGroup(layout.createSequentialGroup()
72+
.addContainerGap()
73+
.addComponent(jScrollPane1,
74+
javax.swing.GroupLayout.DEFAULT_SIZE,
75+
446, Short.MAX_VALUE)
76+
.addContainerGap())
77+
);
78+
layout.setVerticalGroup(
79+
layout.createParallelGroup(
80+
javax.swing.GroupLayout.Alignment.LEADING)
81+
.addGroup(layout.createSequentialGroup()
82+
.addContainerGap()
83+
.addComponent(jScrollPane1)
84+
.addContainerGap())
85+
);
86+
87+
f.pack();
88+
int first = str.indexOf("#");
89+
jTextArea1.setCaretPosition(first);
90+
Caret caret = jTextArea1.getCaret();
91+
rect = jTextArea1.modelToView2D(caret.getDot());
92+
System.out.println("caret x position " + rect.getX());
93+
94+
jTextArea1.setCaretPosition(str.indexOf("#", first+1));
95+
caret = jTextArea1.getCaret();
96+
rect1 = jTextArea1.modelToView2D(caret.getDot());
97+
System.out.println("2nd caret x position " + rect1.getX());
98+
99+
} catch (BadLocationException ex) {
100+
excpnthrown = true;
101+
} finally {
102+
if (f != null) {
103+
f.dispose();
104+
}
105+
}
106+
});
107+
if (excpnthrown) {
108+
throw new RuntimeException("BadLocationException thrown");
109+
}
110+
if ((int)rect.getX() != (int)rect1.getX()) {
111+
throw new RuntimeException("Tab width calculation wrong");
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)