1 /* 2 * Copyright (c) 2010, 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 /* @test 25 * @bug 4743225 26 * @summary Size of JComboBox list is wrong when list is populated via PopupMenuListener 27 * @author Alexander Potochkin 28 */ 29 30 import javax.accessibility.AccessibleContext; 31 import javax.swing.JComboBox; 32 import javax.swing.JFrame; 33 import javax.swing.SwingUtilities; 34 import javax.swing.event.PopupMenuEvent; 35 import javax.swing.event.PopupMenuListener; 36 import javax.swing.plaf.basic.BasicComboPopup; 37 import java.awt.FlowLayout; 38 import java.awt.Point; 39 import java.awt.Robot; 40 import java.awt.event.InputEvent; 41 42 public class bug4743225 extends JFrame { 43 44 private static JComboBox cb; 45 private static volatile boolean flag; 46 47 public bug4743225() { 48 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 49 setLayout(new FlowLayout()); 50 cb = new JComboBox(new Object[] {"one", "two", "three"}); 51 cb.addPopupMenuListener(new PopupMenuListener() { 52 public void popupMenuWillBecomeVisible(PopupMenuEvent e) { 53 cb.addItem("Test"); 54 } 55 56 public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { 57 } 58 59 public void popupMenuCanceled(PopupMenuEvent e) { 60 } 61 }); 62 add(cb); 63 pack(); 64 } 65 66 public static BasicComboPopup getPopup() { 67 AccessibleContext c = cb.getAccessibleContext(); 68 for(int i = 0; i < c.getAccessibleChildrenCount(); i ++) { 69 if (c.getAccessibleChild(i) instanceof BasicComboPopup) { 70 return (BasicComboPopup) c.getAccessibleChild(i); 71 } 72 } 73 throw new AssertionError("No BasicComboPopup found"); 74 } 75 76 public static void main(String... args) throws Exception { 77 78 Robot robot = new Robot(); 79 robot.setAutoDelay(20); 80 81 SwingUtilities.invokeAndWait(new Runnable() { 82 public void run() { 83 new bug4743225().setVisible(true); 84 } 85 }); 86 robot.waitForIdle(); 87 88 // calling this method from main thread is ok 89 Point point = cb.getLocationOnScreen(); 90 robot.mouseMove(point.x + 10, point.y + 10); 91 robot.mousePress(InputEvent.BUTTON1_MASK); 92 robot.mouseRelease(InputEvent.BUTTON1_MASK); 93 robot.waitForIdle(); 94 95 SwingUtilities.invokeAndWait(new Runnable() { 96 public void run() { 97 if(getPopup().getList().getLastVisibleIndex() == 3) { 98 flag = true; 99 } 100 } 101 }); 102 103 if (!flag) { 104 throw new RuntimeException("The ComboBox popup wasn't correctly updated"); 105 } 106 } 107 }