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