Path: blob/master/test/jdk/javax/swing/JComboBox/8032878/bug8032878.java
41154 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @bug 8032878 807885527* @summary Checks that JComboBox as JTable cell editor processes key events28* even where setSurrendersFocusOnKeystroke flag in JTable is false and29* that it does not lose the first key press where the flag is true.30* @run main bug803287831*/3233import java.awt.*;34import java.awt.event.KeyEvent;35import javax.swing.*;36import javax.swing.text.JTextComponent;37import javax.swing.plaf.metal.MetalLookAndFeel;3839public class bug8032878 implements Runnable {40private static final String ONE = "one";41private static final String TWO = "two";42private static final String THREE = "three";4344private static final String EXPECTED = "one123";4546private final Robot robot;4748private JFrame frame;49private JComboBox cb;5051private volatile boolean surrender;52private volatile String text;5354public static void main(String[] args) throws Exception {55UIManager.setLookAndFeel(new MetalLookAndFeel());5657final bug8032878 test = new bug8032878();5859test.test(false);60test.test(true);61}6263public bug8032878() throws AWTException {64robot = new Robot();65robot.setAutoDelay(100);66}6768private void setupUI() {69frame = new JFrame();70frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);7172JTable table = new JTable(new String[][] {{ONE}, {TWO}, {THREE}},73new String[] { "#"});74table.setSurrendersFocusOnKeystroke(surrender);7576cb = new JComboBox(new String[]{ONE, TWO, THREE});77cb.setEditable(true);78DefaultCellEditor comboEditor = new DefaultCellEditor(cb);79comboEditor.setClickCountToStart(1);80table.getColumnModel().getColumn(0).setCellEditor(comboEditor);81frame.add(table);8283frame.pack();84frame.setVisible(true);85frame.setLocationRelativeTo(null);86}8788private void test(final boolean flag) throws Exception {89try {90surrender = flag;91SwingUtilities.invokeAndWait(this);9293robot.waitForIdle();94robot.delay(1000);95runTest();96checkResult();97} finally {98if (frame != null) {99SwingUtilities.invokeAndWait(() -> frame.dispose());100}101}102}103104private void runTest() throws Exception {105robot.waitForIdle();106// Select 'one'107robot.keyPress(KeyEvent.VK_TAB);108robot.keyRelease(KeyEvent.VK_TAB);109robot.waitForIdle();110robot.keyPress(KeyEvent.VK_1);111robot.keyRelease(KeyEvent.VK_1);112robot.waitForIdle();113robot.keyPress(KeyEvent.VK_2);114robot.keyRelease(KeyEvent.VK_2);115robot.waitForIdle();116robot.keyPress(KeyEvent.VK_3);117robot.keyRelease(KeyEvent.VK_3);118robot.waitForIdle();119robot.keyPress(KeyEvent.VK_ENTER);120robot.keyRelease(KeyEvent.VK_ENTER);121robot.waitForIdle();122}123124private void checkResult() throws Exception {125SwingUtilities.invokeAndWait(new Runnable() {126public void run() {127text = ((JTextComponent) cb.getEditor().getEditorComponent()).getText();128}129});130if (text.equals(EXPECTED)) {131System.out.println("Test with surrender = " + surrender + " passed");132} else {133System.out.println("Test with surrender = " + surrender + " failed");134throw new RuntimeException("Expected value in JComboBox editor '" +135EXPECTED + "' but found '" + text + "'.");136}137}138139140@Override141public void run() {142setupUI();143}144}145146147