Skip to content

Commit 54c0178

Browse files
committed
8041705: Bugs in DefaultTreeCellRenderer.updateUI()
Reviewed-by: psadhukhan, pbansal
1 parent 4922a35 commit 54c0178

2 files changed

Lines changed: 106 additions & 4 deletions

File tree

src/java.desktop/share/classes/javax/swing/tree/DefaultTreeCellRenderer.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.awt.Graphics;
3333
import java.awt.Insets;
3434
import java.awt.Rectangle;
35+
import javax.swing.plaf.BorderUIResource.EmptyBorderUIResource;
3536
import javax.swing.plaf.ColorUIResource;
3637
import javax.swing.plaf.FontUIResource;
3738
import javax.swing.plaf.UIResource;
@@ -245,10 +246,13 @@ public void updateUI() {
245246
this, ui, "Tree.drawDashedFocusIndicator", false);
246247

247248
fillBackground = DefaultLookup.getBoolean(this, ui, "Tree.rendererFillBackground", true);
248-
Insets margins = DefaultLookup.getInsets(this, ui, "Tree.rendererMargins");
249-
if (margins != null) {
250-
setBorder(new EmptyBorder(margins.top, margins.left,
251-
margins.bottom, margins.right));
249+
if (!inited || getBorder() instanceof UIResource) {
250+
Insets margins = DefaultLookup.getInsets(this, ui, "Tree.rendererMargins");
251+
if (margins != null) {
252+
setBorder(new EmptyBorderUIResource(margins));
253+
} else {
254+
setBorder(new EmptyBorderUIResource(0, 0, 0, 0));
255+
}
252256
}
253257

254258
setName("Tree.cellRenderer");
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 8041705
27+
* @summary Bugs in DefaultTreeCellRenderer.updateUI()
28+
* @key headful
29+
* @run main DefaultTreeCellRendererBorderTest
30+
*/
31+
32+
import java.awt.BorderLayout;
33+
import java.awt.Insets;
34+
import java.awt.Robot;
35+
import javax.swing.JFrame;
36+
import javax.swing.JTree;
37+
import javax.swing.SwingUtilities;
38+
import javax.swing.UIManager;
39+
import javax.swing.WindowConstants;
40+
import javax.swing.tree.DefaultTreeCellRenderer;
41+
42+
public class DefaultTreeCellRendererBorderTest {
43+
private static JFrame frame;
44+
private static JTree tree;
45+
private static DefaultTreeCellRenderer treeCellRenderer;
46+
private static Robot robot;
47+
private static Insets margin1;
48+
private static Insets margin2;
49+
50+
public static void main(String[] args) throws Exception {
51+
try{
52+
robot = new Robot();
53+
robot.setAutoDelay(50);
54+
SwingUtilities.invokeAndWait(()-> {
55+
frame = new JFrame();
56+
tree = new JTree();
57+
treeCellRenderer = new DefaultTreeCellRenderer();
58+
tree.add(treeCellRenderer);
59+
frame.setDefaultCloseOperation(
60+
WindowConstants.DISPOSE_ON_CLOSE);
61+
frame.setSize(300,300);
62+
frame.setVisible(true);
63+
frame.setLayout(new BorderLayout());
64+
tree.setRootVisible(true);
65+
tree.setShowsRootHandles(true);
66+
frame.add(tree, BorderLayout.CENTER);
67+
});
68+
robot.waitForIdle();
69+
70+
UIManager.setLookAndFeel(
71+
"javax.swing.plaf.nimbus.NimbusLookAndFeel");
72+
UIManager.put("Tree.rendererMargins", new Insets(2, 2, 2, 2));
73+
SwingUtilities.invokeAndWait(() -> {
74+
SwingUtilities.updateComponentTreeUI(frame);
75+
margin1 = treeCellRenderer.getInsets();
76+
});
77+
robot.waitForIdle();
78+
79+
UIManager.put("Tree.rendererMargins", null);
80+
UIManager.setLookAndFeel(
81+
"javax.swing.plaf.metal.MetalLookAndFeel");
82+
SwingUtilities.invokeAndWait(()->{
83+
SwingUtilities.updateComponentTreeUI(frame);
84+
margin2 = treeCellRenderer.getInsets();
85+
});
86+
robot.waitForIdle();
87+
88+
if(margin1.equals(margin2)) {
89+
throw new RuntimeException("Test Failed : NimbusLookAndFeel "+
90+
"Border persists for MetalLookAndFeel");
91+
}
92+
} finally {
93+
if(frame != null) {
94+
SwingUtilities.invokeAndWait(frame::dispose);
95+
}
96+
}
97+
}
98+
}

0 commit comments

Comments
 (0)