|
| 1 | +/* |
| 2 | + * Copyright (c) 1999, 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 4139900 |
| 27 | + * @summary height of combobox may differ |
| 28 | + * @key headful |
| 29 | + * @run main bug4139900 |
| 30 | +*/ |
| 31 | + |
| 32 | +import java.awt.Dimension; |
| 33 | +import java.awt.Robot; |
| 34 | +import java.awt.event.ActionListener; |
| 35 | +import javax.swing.DefaultComboBoxModel; |
| 36 | +import javax.swing.JButton; |
| 37 | +import javax.swing.JComboBox; |
| 38 | +import javax.swing.JFrame; |
| 39 | +import javax.swing.JPanel; |
| 40 | +import javax.swing.SwingUtilities; |
| 41 | + |
| 42 | +public class bug4139900 { |
| 43 | + static JButton button; |
| 44 | + static JFrame frame; |
| 45 | + static JComboBox<String> comboBox; |
| 46 | + static int initialHeight; |
| 47 | + |
| 48 | + public static void main(String[] args) throws Exception { |
| 49 | + try { |
| 50 | + SwingUtilities.invokeAndWait(bug4139900::init); |
| 51 | + test(); |
| 52 | + } finally { |
| 53 | + SwingUtilities.invokeAndWait(() -> { |
| 54 | + if (frame != null) { |
| 55 | + frame.dispose(); |
| 56 | + } |
| 57 | + }); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private static void test() throws Exception { |
| 62 | + Robot robot = new Robot(); |
| 63 | + robot.waitForIdle(); |
| 64 | + robot.delay(500); |
| 65 | + |
| 66 | + SwingUtilities.invokeAndWait(() -> initialHeight = comboBox.getHeight()); |
| 67 | + |
| 68 | + for (int i = 0; i < 10; i++) { |
| 69 | + SwingUtilities.invokeAndWait(() -> button.doClick()); |
| 70 | + robot.waitForIdle(); |
| 71 | + robot.delay(200); |
| 72 | + SwingUtilities.invokeAndWait(() -> { |
| 73 | + if (comboBox.getHeight() != initialHeight) { |
| 74 | + throw new RuntimeException( |
| 75 | + "Test failed: height differs from initial %d != %d" |
| 76 | + .formatted(comboBox.getHeight(), initialHeight) |
| 77 | + ); |
| 78 | + } |
| 79 | + }); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public static void init() { |
| 84 | + frame = new JFrame("bug4139900"); |
| 85 | + |
| 86 | + DefaultComboBoxModel<String> model = |
| 87 | + new DefaultComboBoxModel<>(new String[]{ |
| 88 | + "Coma Berenices", |
| 89 | + "Triangulum", |
| 90 | + "Camelopardis", |
| 91 | + "Cassiopea" |
| 92 | + }); |
| 93 | + |
| 94 | + comboBox = new JComboBox<>(); |
| 95 | + comboBox.setEditable(true); |
| 96 | + |
| 97 | + button = new JButton("Add/Remove Items"); |
| 98 | + |
| 99 | + ActionListener actionListener = e -> { |
| 100 | + if (comboBox.getModel() == model) { |
| 101 | + comboBox.setModel(new DefaultComboBoxModel<>()); |
| 102 | + } else { |
| 103 | + comboBox.setModel(model); |
| 104 | + } |
| 105 | + }; |
| 106 | + |
| 107 | + button.addActionListener(actionListener); |
| 108 | + |
| 109 | + JPanel panel = new JPanel(); |
| 110 | + panel.setPreferredSize(new Dimension(300, 100)); |
| 111 | + panel.add(comboBox); |
| 112 | + panel.add(button); |
| 113 | + |
| 114 | + frame.add(panel); |
| 115 | + frame.pack(); |
| 116 | + frame.setLocationRelativeTo(null); |
| 117 | + frame.setVisible(true); |
| 118 | + } |
| 119 | +} |
0 commit comments