Path: blob/master/test/jdk/javax/swing/JComboBox/6607130/bug6607130.java
41153 views
/*1* Copyright (c) 2008, 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 660713027* @summary Checks that JComboBox cell editor is hidden if the same28* item is selected with keyboard.29* Also checks that JComboBox cell editor is hidden if F2 and then30* ENTER were pressed.31* @author Mikhail Lapshin32*/3334import javax.swing.*;35import javax.swing.table.DefaultTableModel;36import java.awt.*;37import java.awt.event.KeyEvent;3839public class bug6607130 {40private JFrame frame;41private JComboBox cb;42private Robot robot;4344public static void main(String[] args) throws Exception {45final bug6607130 test = new bug6607130();46try {47SwingUtilities.invokeAndWait(new Runnable() {48public void run() {49test.setupUI();50}51});52test.test();53} finally {54if (test.frame != null) {55test.frame.dispose();56}57}58}5960public bug6607130() throws AWTException {61robot = new Robot();62}6364private void setupUI() {65frame = new JFrame();66frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);6768DefaultTableModel model = new DefaultTableModel(1, 1);69JTable table = new JTable(model);7071cb = new JComboBox(new String[]{"one", "two", "three"});72table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(cb));73frame.add(table);7475frame.pack();76frame.setLocationRelativeTo(null);77frame.setVisible(true);78}7980private void test() throws Exception {81robot.waitForIdle();82test1();83robot.waitForIdle();84checkResult("First test");85test2();86robot.waitForIdle();87checkResult("Second test");88}8990private void test1() throws Exception {91// Select 'one'92hitKey(KeyEvent.VK_TAB);93robot.waitForIdle();94hitKey(KeyEvent.VK_F2);95robot.waitForIdle();96hitKey(KeyEvent.VK_DOWN);97robot.waitForIdle();98hitKey(KeyEvent.VK_DOWN);99robot.waitForIdle();100hitKey(KeyEvent.VK_ENTER);101robot.waitForIdle();102103// Select 'one' again104hitKey(KeyEvent.VK_F2);105robot.waitForIdle();106hitKey(KeyEvent.VK_DOWN);107robot.waitForIdle();108hitKey(KeyEvent.VK_ENTER);109robot.waitForIdle();110}111112private void test2() throws Exception {113// Press F2 and then press ENTER114// Editor should be shown and then closed115hitKey(KeyEvent.VK_F2);116robot.waitForIdle();117hitKey(KeyEvent.VK_ENTER);118robot.waitForIdle();119}120121private void checkResult(String testName) {122if (!cb.isShowing()) {123System.out.println(testName + " passed");124} else {125System.out.println(testName + " failed");126throw new RuntimeException("JComboBox is showing " +127"after item selection.");128}129}130131public void hitKey(int keycode) {132robot.keyPress(keycode);133robot.keyRelease(keycode);134delay();135}136137private void delay() {138try {139Thread.sleep(1000);140} catch(InterruptedException ie) {141ie.printStackTrace();142}143}144}145146147