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