Skip to content

Commit 8082c24

Browse files
Damon Nguyenprsadhuk
Damon Nguyen
authored andcommitted
8054572: [macosx] JComboBox paints the border incorrectly
Reviewed-by: honkar, psadhukhan
1 parent b920d29 commit 8082c24

File tree

2 files changed

+121
-3
lines changed

2 files changed

+121
-3
lines changed

src/java.desktop/macosx/classes/com/apple/laf/AquaComboBoxUI.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2022, 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
@@ -453,6 +453,31 @@ protected LayoutManager createLayoutManager() {
453453
}
454454

455455
class AquaComboBoxLayoutManager extends BasicComboBoxUI.ComboBoxLayoutManager {
456+
protected Rectangle rectangleForCurrentValue() {
457+
int width = comboBox.getWidth();
458+
int height = 22;
459+
Insets insets = getInsets();
460+
int buttonSize = height - (insets.top + insets.bottom);
461+
if ( arrowButton != null ) {
462+
buttonSize = arrowButton.getWidth();
463+
}
464+
int midHeight = (comboBox.getHeight() - height - (insets.top + insets.bottom)) / 2 - 1;
465+
if (midHeight < 0) {
466+
midHeight = 0;
467+
}
468+
469+
if (comboBox.getComponentOrientation().isLeftToRight()) {
470+
return new Rectangle(insets.left, insets.top + midHeight,
471+
width - (insets.left + insets.right + buttonSize) + 4,
472+
height - (insets.top + insets.bottom));
473+
}
474+
else {
475+
return new Rectangle(insets.left + buttonSize, insets.top + midHeight,
476+
width - (insets.left + insets.right + buttonSize) + 4,
477+
height - (insets.top + insets.bottom));
478+
}
479+
}
480+
456481
public void layoutContainer(final Container parent) {
457482
if (arrowButton != null && !comboBox.isEditable()) {
458483
final Insets insets = comboBox.getInsets();
@@ -476,8 +501,6 @@ public void layoutContainer(final Container parent) {
476501

477502
if (editor != null) {
478503
final Rectangle editorRect = rectangleForCurrentValue();
479-
editorRect.width += 4;
480-
editorRect.height += 1;
481504
editor.setBounds(editorRect);
482505
}
483506
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright (c) 2022, 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 8054572
27+
* @library /java/awt/regtesthelpers
28+
* @build PassFailJFrame
29+
* @summary Tests if JComboBox displays correctly when editable/non-editable
30+
* @run main/manual JComboBoxBorderTest
31+
*/
32+
33+
import java.awt.FlowLayout;
34+
import java.lang.reflect.InvocationTargetException;
35+
36+
import javax.swing.JComboBox;
37+
import javax.swing.JFrame;
38+
import javax.swing.JLabel;
39+
import javax.swing.JPanel;
40+
import javax.swing.SwingUtilities;
41+
import javax.swing.UIManager;
42+
43+
public class JComboBoxBorderTest {
44+
private static final String instructionsText = "Pass if you can see both " +
45+
"an editable and non-editable JComboBox and if they display " +
46+
"reasonably. Fail if they do not appear or are misaligned.";
47+
48+
private static JFrame frame;
49+
50+
public static void createAndShowGUI() throws InterruptedException,
51+
InvocationTargetException {
52+
SwingUtilities.invokeAndWait(() -> {
53+
54+
JLabel label = new JLabel("Editable combo box:");
55+
JLabel label2 = new JLabel("Non-editable combo box:");
56+
57+
JComboBox<String> comboBox = new JComboBox<>(new String[]
58+
{ "Item 1", "Item 2", "Item 3" });
59+
JComboBox<String> comboBox2 = new JComboBox<>(new String[]
60+
{ "Item 1", "Item 2", "Item 3" });
61+
comboBox.setEditable(true);
62+
63+
FlowLayout layout = new FlowLayout(FlowLayout.LEADING);
64+
JPanel panel = new JPanel(layout);
65+
panel.add(label);
66+
panel.add(comboBox);
67+
68+
panel.add(label2);
69+
panel.add(comboBox2);
70+
71+
frame = new JFrame();
72+
frame.getContentPane().add(panel);
73+
frame.pack();
74+
frame.setLocationRelativeTo(null);
75+
76+
PassFailJFrame.addTestWindow(frame);
77+
PassFailJFrame.positionTestWindow(frame,
78+
PassFailJFrame.Position.HORIZONTAL);
79+
80+
frame.setVisible(true);
81+
});
82+
}
83+
84+
public static void main(String[] args) throws Exception {
85+
86+
UIManager.setLookAndFeel("com.apple.laf.AquaLookAndFeel");
87+
88+
PassFailJFrame pfjFrame = new PassFailJFrame("JScrollPane "
89+
+ "Test Instructions", instructionsText, 5);
90+
91+
createAndShowGUI();
92+
93+
pfjFrame.awaitAndCheck();
94+
}
95+
}

0 commit comments

Comments
 (0)