Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JComboBox/6607130/bug6607130.java
38853 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/* @test24* @bug 660713025* @summary Checks that JComboBox cell editor is hidden if the same26* item is selected with keyboard.27* Also checks that JComboBox cell editor is hidden if F2 and then28* ENTER were pressed.29* @author Mikhail Lapshin30*/3132import javax.swing.*;33import javax.swing.table.DefaultTableModel;34import java.awt.*;35import java.awt.event.KeyEvent;3637public class bug6607130 {38private JFrame frame;39private JComboBox cb;40private Robot robot;4142public static void main(String[] args) throws Exception {43final bug6607130 test = new bug6607130();44try {45SwingUtilities.invokeAndWait(new Runnable() {46public void run() {47test.setupUI();48}49});50test.test();51} finally {52if (test.frame != null) {53test.frame.dispose();54}55}56}5758public bug6607130() throws AWTException {59robot = new Robot();60}6162private void setupUI() {63frame = new JFrame();64frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);6566DefaultTableModel model = new DefaultTableModel(1, 1);67JTable table = new JTable(model);6869cb = new JComboBox(new String[]{"one", "two", "three"});70table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(cb));71frame.add(table);7273frame.pack();74frame.setLocationRelativeTo(null);75frame.setVisible(true);76}7778private void test() throws Exception {79robot.waitForIdle();80test1();81robot.waitForIdle();82checkResult("First test");83test2();84robot.waitForIdle();85checkResult("Second test");86}8788private void test1() throws Exception {89// Select 'one'90hitKey(KeyEvent.VK_TAB);91robot.waitForIdle();92hitKey(KeyEvent.VK_F2);93robot.waitForIdle();94hitKey(KeyEvent.VK_DOWN);95robot.waitForIdle();96hitKey(KeyEvent.VK_DOWN);97robot.waitForIdle();98hitKey(KeyEvent.VK_ENTER);99robot.waitForIdle();100101// Select 'one' again102hitKey(KeyEvent.VK_F2);103robot.waitForIdle();104hitKey(KeyEvent.VK_DOWN);105robot.waitForIdle();106hitKey(KeyEvent.VK_ENTER);107robot.waitForIdle();108}109110private void test2() throws Exception {111// Press F2 and then press ENTER112// Editor should be shown and then closed113hitKey(KeyEvent.VK_F2);114robot.waitForIdle();115hitKey(KeyEvent.VK_ENTER);116robot.waitForIdle();117}118119private void checkResult(String testName) {120if (!cb.isShowing()) {121System.out.println(testName + " passed");122} else {123System.out.println(testName + " failed");124throw new RuntimeException("JComboBox is showing " +125"after item selection.");126}127}128129public void hitKey(int keycode) {130robot.keyPress(keycode);131robot.keyRelease(keycode);132delay();133}134135private void delay() {136try {137Thread.sleep(1000);138} catch(InterruptedException ie) {139ie.printStackTrace();140}141}142}143144145