1 /* 2 * Copyright (c) 2014, 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 * @key headful 26 * @bug 8032878 27 * @summary Checks that JComboBox as JTable cell editor processes key events 28 * even where setSurrendersFocusOnKeystroke flag in JTable is false and 29 * that it does not lose the first key press where the flag is true. 30 * @library ../../regtesthelpers 31 * @build Util 32 * @author Alexey Ivanov 33 */ 34 35 import java.awt.*; 36 import java.awt.event.KeyEvent; 37 import javax.swing.*; 38 import javax.swing.text.JTextComponent; 39 40 import sun.awt.SunToolkit; 41 42 public class bug8032878 implements Runnable { 43 private static final String ONE = "one"; 44 private static final String TWO = "two"; 45 private static final String THREE = "three"; 46 47 private static final String EXPECTED = "one123"; 48 49 private final Robot robot; 50 51 private JFrame frame; 52 private JComboBox cb; 53 54 private volatile boolean surrender; 55 private volatile String text; 56 57 public static void main(String[] args) throws Exception { 58 final bug8032878 test = new bug8032878(); 59 60 test.test(false); 61 test.test(true); 62 } 63 64 public bug8032878() throws AWTException { 65 robot = new Robot(); 66 robot.setAutoDelay(100); 67 } 68 69 private void setupUI() { 70 frame = new JFrame(); 71 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 72 73 JTable table = new JTable(new String[][] {{ONE}, {TWO}, {THREE}}, 74 new String[] { "#"}); 75 table.setSurrendersFocusOnKeystroke(surrender); 76 77 cb = new JComboBox(new String[]{ONE, TWO, THREE}); 78 cb.setEditable(true); 79 DefaultCellEditor comboEditor = new DefaultCellEditor(cb); 80 comboEditor.setClickCountToStart(1); 81 table.getColumnModel().getColumn(0).setCellEditor(comboEditor); 82 frame.add(table); 83 84 frame.pack(); 85 frame.setVisible(true); 86 } 87 88 private void test(final boolean flag) throws Exception { 89 try { 90 surrender = flag; 91 SwingUtilities.invokeAndWait(this); 92 93 runTest(); 94 checkResult(); 95 } finally { 96 if (frame != null) { 97 frame.dispose(); 98 } 99 } 100 } 101 102 private void runTest() throws Exception { 103 realSync(); 104 // Select 'one' 105 Util.hitKeys(robot, KeyEvent.VK_TAB); 106 realSync(); 107 Util.hitKeys(robot, KeyEvent.VK_1); 108 Util.hitKeys(robot, KeyEvent.VK_2); 109 Util.hitKeys(robot, KeyEvent.VK_3); 110 Util.hitKeys(robot, KeyEvent.VK_ENTER); 111 realSync(); 112 } 113 114 private void checkResult() throws Exception { 115 SwingUtilities.invokeAndWait(new Runnable() { 116 public void run() { 117 text = ((JTextComponent) cb.getEditor().getEditorComponent()).getText(); 118 } 119 }); 120 if (text.equals(EXPECTED)) { 121 System.out.println("Test with surrender = " + surrender + " passed"); 122 } else { 123 System.out.println("Test with surrender = " + surrender + " failed"); 124 throw new RuntimeException("Expected value in JComboBox editor '" + 125 EXPECTED + "' but found '" + text + "'."); 126 } 127 } 128 129 private static void realSync() { 130 ((SunToolkit) (Toolkit.getDefaultToolkit())).realSync(); 131 } 132 133 @Override 134 public void run() { 135 setupUI(); 136 } 137 }