Skip to content

Commit 674cc3e

Browse files
committed
8042054: JTree.updateUI uses out-of-date item size information
Reviewed-by: dnguyen, serb
1 parent 7e91d34 commit 674cc3e

File tree

2 files changed

+99
-3
lines changed

2 files changed

+99
-3
lines changed

src/java.desktop/share/classes/javax/swing/JTree.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2025, 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
@@ -777,10 +777,10 @@ public void updateUI() {
777777
updateInProgress = true;
778778

779779
try {
780-
setUI((TreeUI)UIManager.getUI(this));
781-
782780
SwingUtilities.updateRendererOrEditorUI(getCellRenderer());
783781
SwingUtilities.updateRendererOrEditorUI(getCellEditor());
782+
783+
setUI((TreeUI)UIManager.getUI(this));
784784
} finally {
785785
updateInProgress = false;
786786
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (c) 2025, 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 8042054
27+
* @summary JTree.updateUI should use updated item size information
28+
* @library /java/awt/regtesthelpers
29+
* @build PassFailJFrame
30+
* @run main/manual JTreeUpdateTest
31+
*/
32+
33+
import java.awt.BorderLayout;
34+
import java.awt.Dimension;
35+
import javax.swing.BorderFactory;
36+
import javax.swing.JFrame;
37+
import javax.swing.JTree;
38+
import javax.swing.SwingUtilities;
39+
import javax.swing.tree.DefaultTreeCellRenderer;
40+
import javax.swing.UIManager;
41+
42+
public class JTreeUpdateTest {
43+
44+
static final String INSTRUCTIONS = """
45+
A frame with two identical JTrees is shown.
46+
If the left JTree's text is abbreviated and JTree items
47+
are cramped with little space between rows then press Fail.
48+
If the left JTree is identical with right JTree, press Pass.
49+
""";
50+
51+
public static void main(String[] args) throws Exception {
52+
PassFailJFrame.builder()
53+
.title("JTreeUpdateTest Test instructions")
54+
.instructions(INSTRUCTIONS)
55+
.columns(30)
56+
.testUI(JTreeUpdateTest::createUI)
57+
.build()
58+
.awaitAndCheck();
59+
}
60+
61+
private static JFrame createUI() {
62+
setLaf("javax.swing.plaf.metal.MetalLookAndFeel");
63+
64+
final JFrame frame = new JFrame("JTreeUpdateTest");
65+
66+
final JTree tree = new JTree();
67+
tree.setPreferredSize(new Dimension(200, 200));
68+
tree.setCellRenderer(new DefaultTreeCellRenderer());
69+
tree.setBorder(BorderFactory.createTitledBorder("updateUI() called once"));
70+
frame.add(tree);
71+
72+
final JTree tree2 = new JTree();
73+
tree2.setPreferredSize(new Dimension(200, 200));
74+
tree2.setCellRenderer(new DefaultTreeCellRenderer());
75+
tree2.setBorder(BorderFactory.createTitledBorder("updateUI() called twice"));
76+
frame.add(tree2, BorderLayout.EAST);
77+
78+
frame.pack();
79+
frame.setLocationRelativeTo(null);
80+
81+
SwingUtilities.invokeLater(() -> {
82+
setLaf("javax.swing.plaf.nimbus.NimbusLookAndFeel");
83+
SwingUtilities.updateComponentTreeUI(frame);
84+
SwingUtilities.updateComponentTreeUI(tree2);
85+
});
86+
return frame;
87+
}
88+
89+
private static void setLaf(String className) {
90+
try {
91+
UIManager.setLookAndFeel(className);
92+
} catch (Exception e) {
93+
throw new RuntimeException(e);
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)