Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JComboBox/8032878/bug8032878.java
38854 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/* @test24* @bug 803287825* @summary Checks that JComboBox as JTable cell editor processes key events26* even where setSurrendersFocusOnKeystroke flag in JTable is false and27* that it does not lose the first key press where the flag is true.28* @library ../../regtesthelpers29* @build Util30* @author Alexey Ivanov31* @run main bug803287832*/3334import java.awt.*;35import java.awt.event.KeyEvent;36import javax.swing.*;37import javax.swing.text.JTextComponent;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 {55final bug8032878 test = new bug8032878();5657test.test(false);58test.test(true);59}6061public bug8032878() throws AWTException {62robot = new Robot();63robot.setAutoDelay(100);64}6566private void setupUI() {67frame = new JFrame();68frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);6970JTable table = new JTable(new String[][] {{ONE}, {TWO}, {THREE}},71new String[] { "#"});72table.setSurrendersFocusOnKeystroke(surrender);7374cb = new JComboBox(new String[]{ONE, TWO, THREE});75cb.setEditable(true);76DefaultCellEditor comboEditor = new DefaultCellEditor(cb);77comboEditor.setClickCountToStart(1);78table.getColumnModel().getColumn(0).setCellEditor(comboEditor);79frame.add(table);8081frame.pack();82frame.setVisible(true);83}8485private void test(final boolean flag) throws Exception {86try {87surrender = flag;88SwingUtilities.invokeAndWait(this);8990runTest();91checkResult();92} finally {93if (frame != null) {94frame.dispose();95}96}97}9899private void runTest() throws Exception {100robot.waitForIdle();101// Select 'one'102Util.hitKeys(robot, KeyEvent.VK_TAB);103robot.waitForIdle();104Util.hitKeys(robot, KeyEvent.VK_1);105Util.hitKeys(robot, KeyEvent.VK_2);106Util.hitKeys(robot, KeyEvent.VK_3);107Util.hitKeys(robot, KeyEvent.VK_ENTER);108robot.waitForIdle();109}110111private void checkResult() throws Exception {112SwingUtilities.invokeAndWait(new Runnable() {113public void run() {114text = ((JTextComponent) cb.getEditor().getEditorComponent()).getText();115}116});117if (text.equals(EXPECTED)) {118System.out.println("Test with surrender = " + surrender + " passed");119} else {120System.out.println("Test with surrender = " + surrender + " failed");121throw new RuntimeException("Expected value in JComboBox editor '" +122EXPECTED + "' but found '" + text + "'.");123}124}125126127@Override128public void run() {129setupUI();130}131}132133134