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