Path: blob/master/test/jdk/javax/swing/JComboBox/6559152/bug6559152.java
41153 views
/*1* Copyright (c) 2011, 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 655915227* @summary Checks that you can select an item in JComboBox with keyboard28* when it is a JTable cell editor.29* @run main bug655915230*/3132import javax.swing.DefaultCellEditor;33import javax.swing.JComponent;34import javax.swing.JFrame;35import javax.swing.JComboBox;36import javax.swing.SwingUtilities;37import javax.swing.table.DefaultTableModel;38import javax.swing.JTable;39import java.awt.Point;40import java.awt.event.KeyEvent;41import java.awt.event.InputEvent;42import java.awt.Robot;4344public class bug6559152 {45private static JFrame frame;46private static JComboBox cb;47private static Robot robot;48private static Point p = null;4950public static void main(String[] args) throws Exception {51robot = new Robot();52robot.setAutoDelay(100);53try {54SwingUtilities.invokeAndWait(() -> setupUI());55blockTillDisplayed(cb);56robot.waitForIdle();57robot.delay(1000);58test();59} finally {60if (frame != null) {61SwingUtilities.invokeAndWait(() -> frame.dispose());62}63}64}6566static void blockTillDisplayed(JComponent comp) throws Exception {67while (p == null) {68try {69SwingUtilities.invokeAndWait(() -> {70p = comp.getLocationOnScreen();71});72} catch (IllegalStateException e) {73try {74Thread.sleep(1000);75} catch (InterruptedException ie) {76}77}78}79}8081private static void setupUI() {82frame = new JFrame();83frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);8485DefaultTableModel model = new DefaultTableModel(1, 1);86JTable table = new JTable(model);8788cb = new JComboBox(new String[]{"one", "two", "three"});89cb.setEditable(true);90table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(cb));91frame.add(cb);9293frame.pack();94frame.setLocationRelativeTo(null);95frame.setVisible(true);96}9798private static void test() throws Exception {99robot.mouseMove(p.x, p.y);100robot.waitForIdle();101robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);102robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);103robot.waitForIdle();104testImpl();105checkResult();106}107108private static void testImpl() throws Exception {109robot.keyPress(KeyEvent.VK_DOWN);110robot.keyRelease(KeyEvent.VK_DOWN);111robot.waitForIdle();112robot.keyPress(KeyEvent.VK_DOWN);113robot.keyRelease(KeyEvent.VK_DOWN);114robot.waitForIdle();115robot.keyPress(KeyEvent.VK_ENTER);116robot.keyRelease(KeyEvent.VK_ENTER);117robot.waitForIdle();118}119120private static void checkResult() {121if (cb.getSelectedItem().equals("two")) {122System.out.println("Test passed");123} else {124System.out.println("Test failed");125throw new RuntimeException("Cannot select an item " +126"from popup with the ENTER key.");127}128}129}130131132