1 /* 2 * Copyright (c) 2008, 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 * @key headful 27 * @bug 6607130 28 * @summary Checks that JComboBox cell editor is hidden if the same 29 * item is selected with keyboard. 30 * Also checks that JComboBox cell editor is hidden if F2 and then 31 * ENTER were pressed. 32 * @author Mikhail Lapshin 33 */ 34 35 import sun.awt.SunToolkit; 36 37 import javax.swing.*; 38 import javax.swing.table.DefaultTableModel; 39 import java.awt.*; 40 import java.awt.event.KeyEvent; 41 42 public class bug6607130 { 43 private JFrame frame; 44 private JComboBox cb; 45 private Robot robot; 46 47 public static void main(String[] args) throws Exception { 48 final bug6607130 test = new bug6607130(); 49 try { 50 SwingUtilities.invokeAndWait(new Runnable() { 51 public void run() { 52 test.setupUI(); 53 } 54 }); 55 test.test(); 56 } finally { 57 if (test.frame != null) { 58 test.frame.dispose(); 59 } 60 } 61 } 62 63 public bug6607130() throws AWTException { 64 robot = new Robot(); 65 } 66 67 private void setupUI() { 68 frame = new JFrame(); 69 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 70 71 DefaultTableModel model = new DefaultTableModel(1, 1); 72 JTable table = new JTable(model); 73 74 cb = new JComboBox(new String[]{"one", "two", "three"}); 75 table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(cb)); 76 frame.add(table); 77 78 frame.pack(); 79 frame.setLocationRelativeTo(null); 80 frame.setVisible(true); 81 } 82 83 private void test() throws Exception { 84 realSync(); 85 test1(); 86 realSync(); 87 checkResult("First test"); 88 test2(); 89 realSync(); 90 checkResult("Second test"); 91 } 92 93 private void test1() throws Exception { 94 // Select 'one' 95 hitKey(KeyEvent.VK_TAB); 96 realSync(); 97 hitKey(KeyEvent.VK_F2); 98 realSync(); 99 hitKey(KeyEvent.VK_DOWN); 100 realSync(); 101 hitKey(KeyEvent.VK_DOWN); 102 realSync(); 103 hitKey(KeyEvent.VK_ENTER); 104 realSync(); 105 106 // Select 'one' again 107 hitKey(KeyEvent.VK_F2); 108 realSync(); 109 hitKey(KeyEvent.VK_DOWN); 110 realSync(); 111 hitKey(KeyEvent.VK_ENTER); 112 realSync(); 113 } 114 115 private void test2() throws Exception { 116 // Press F2 and then press ENTER 117 // Editor should be shown and then closed 118 hitKey(KeyEvent.VK_F2); 119 realSync(); 120 hitKey(KeyEvent.VK_ENTER); 121 realSync(); 122 } 123 124 private void checkResult(String testName) { 125 if (!cb.isShowing()) { 126 System.out.println(testName + " passed"); 127 } else { 128 System.out.println(testName + " failed"); 129 throw new RuntimeException("JComboBox is showing " + 130 "after item selection."); 131 } 132 } 133 134 private static void realSync() { 135 ((SunToolkit) (Toolkit.getDefaultToolkit())).realSync(); 136 } 137 138 public void hitKey(int keycode) { 139 robot.keyPress(keycode); 140 robot.keyRelease(keycode); 141 delay(); 142 } 143 144 private void delay() { 145 try { 146 Thread.sleep(1000); 147 } catch(InterruptedException ie) { 148 ie.printStackTrace(); 149 } 150 } 151 }