Skip to content

Commit 2bd2fae

Browse files
committed
4346610: Adding JSeparator to JToolBar "pushes" buttons added after separator to edge
Reviewed-by: tr, aivanov, dnguyen
1 parent 6a15860 commit 2bd2fae

File tree

2 files changed

+111
-5
lines changed

2 files changed

+111
-5
lines changed

src/java.desktop/share/classes/javax/swing/plaf/basic/BasicSeparatorUI.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2023, 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
@@ -25,12 +25,12 @@
2525

2626
package javax.swing.plaf.basic;
2727

28-
import javax.swing.*;
2928
import java.awt.Color;
3029
import java.awt.Dimension;
3130
import java.awt.Graphics;
32-
import java.awt.Insets;
33-
import java.awt.Rectangle;
31+
import javax.swing.JComponent;
32+
import javax.swing.JSeparator;
33+
import javax.swing.LookAndFeel;
3434
import javax.swing.plaf.ComponentUI;
3535
import javax.swing.plaf.SeparatorUI;
3636

@@ -152,5 +152,13 @@ public Dimension getPreferredSize( JComponent c )
152152
}
153153

154154
public Dimension getMinimumSize( JComponent c ) { return null; }
155-
public Dimension getMaximumSize( JComponent c ) { return null; }
155+
156+
public Dimension getMaximumSize( JComponent c ) {
157+
Dimension d = getPreferredSize(c);
158+
if (((JSeparator)c).getOrientation() == JSeparator.VERTICAL) {
159+
return new Dimension(d.width, Short.MAX_VALUE);
160+
} else {
161+
return new Dimension(Short.MAX_VALUE, d.height);
162+
}
163+
}
156164
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright (c) 2023, 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 4346610
27+
* @key headful
28+
* @summary Verifies if Adding JSeparator to JToolBar "pushes" buttons added
29+
* after separator to edge
30+
* @run main ToolBarSeparatorSizeTest
31+
*/
32+
import java.awt.BorderLayout;
33+
import java.awt.image.BufferedImage;
34+
import java.awt.Rectangle;
35+
import java.awt.Robot;
36+
37+
import javax.swing.JButton;
38+
import javax.swing.JFrame;
39+
import javax.swing.JPanel;
40+
import javax.swing.JSeparator;
41+
import javax.swing.JToolBar;
42+
import javax.swing.SwingConstants;
43+
import javax.swing.SwingUtilities;
44+
45+
import javax.imageio.ImageIO;
46+
47+
public class ToolBarSeparatorSizeTest {
48+
49+
private static JFrame frame;
50+
private static JSeparator separator;
51+
private static JToolBar toolBar;
52+
private static volatile Rectangle toolBarBounds;
53+
private static volatile int sepWidth;
54+
private static volatile int sepPrefWidth;
55+
56+
public static void main(String[] args) throws Exception {
57+
Robot robot = new Robot();
58+
robot.setAutoDelay(100);
59+
try {
60+
SwingUtilities.invokeAndWait(() -> {
61+
frame = new JFrame("ToolBar Separator Test");
62+
toolBar = new JToolBar();
63+
toolBar.add(new JButton("button 1"));
64+
toolBar.add(new JButton("button 2"));
65+
separator = new JSeparator(SwingConstants.VERTICAL);
66+
toolBar.add(separator);
67+
toolBar.add(new JButton("button 3"));
68+
frame.getContentPane().setLayout(new BorderLayout());
69+
frame.getContentPane().add(toolBar, BorderLayout.NORTH);
70+
frame.getContentPane().add(new JPanel(), BorderLayout.CENTER);
71+
frame.setSize(400, 100);
72+
frame.setLocationRelativeTo(null);
73+
frame.setVisible(true);
74+
});
75+
robot.waitForIdle();
76+
robot.delay(1000);
77+
SwingUtilities.invokeAndWait(() -> {
78+
toolBarBounds = new Rectangle(toolBar.getLocationOnScreen(),
79+
toolBar.getSize());
80+
sepWidth = separator.getSize().width;
81+
sepPrefWidth = separator.getPreferredSize().width;
82+
});
83+
if (sepWidth != sepPrefWidth) {
84+
System.out.println("size " + sepWidth);
85+
System.out.println("preferredSize " + sepPrefWidth);
86+
BufferedImage img = robot.createScreenCapture(toolBarBounds);
87+
ImageIO.write(img, "png", new java.io.File("image.png"));
88+
throw new RuntimeException("Separator size is too wide");
89+
}
90+
} finally {
91+
SwingUtilities.invokeAndWait(() -> {
92+
if (frame != null) {
93+
frame.dispose();
94+
}
95+
});
96+
}
97+
}
98+
}

0 commit comments

Comments
 (0)