From 286f83f41decfaef5df81b5e20d1be1be1926b73 Mon Sep 17 00:00:00 2001 From: mlapshin Date: Mon, 21 Jul 2008 19:58:43 +0400 Subject: [PATCH] 6607130: REGRESSION: JComboBox cell editor isn't hidden if the same value is selected with keyboard Summary: JComboBox cell editor now hides if the same value is selected with keyboard Reviewed-by: peterz, alexp --- .../swing/plaf/basic/BasicComboBoxUI.java | 12 +- .../swing/JComboBox/6607130/bug6607130.java | 149 ++++++++++++++++++ 2 files changed, 158 insertions(+), 3 deletions(-) create mode 100644 test/javax/swing/JComboBox/6607130/bug6607130.java diff --git a/src/share/classes/javax/swing/plaf/basic/BasicComboBoxUI.java b/src/share/classes/javax/swing/plaf/basic/BasicComboBoxUI.java index f073eeda8..c49387fd9 100644 --- a/src/share/classes/javax/swing/plaf/basic/BasicComboBoxUI.java +++ b/src/share/classes/javax/swing/plaf/basic/BasicComboBoxUI.java @@ -1509,15 +1509,21 @@ public class BasicComboBoxUI extends ComboBoxUI { || ui.isTableCellEditor) { Object listItem = ui.popup.getList().getSelectedValue(); if (listItem != null) { - comboBox.getModel().setSelectedItem(listItem); - // Ensure that JComboBox.actionPerformed() - // doesn't set editor value as selected item + // Use the selected value from popup + // to set the selected item in combo box, + // but ensure before that JComboBox.actionPerformed() + // won't use editor's value to set the selected item comboBox.getEditor().setItem(listItem); + comboBox.setSelectedItem(listItem); } } comboBox.setPopupVisible(false); } else { + // Hide combo box if it is a table cell editor + if (ui.isTableCellEditor && !comboBox.isEditable()) { + comboBox.setSelectedItem(comboBox.getSelectedItem()); + } // Call the default button binding. // This is a pretty messy way of passing an event through // to the root pane. diff --git a/test/javax/swing/JComboBox/6607130/bug6607130.java b/test/javax/swing/JComboBox/6607130/bug6607130.java new file mode 100644 index 000000000..5d465c9d7 --- /dev/null +++ b/test/javax/swing/JComboBox/6607130/bug6607130.java @@ -0,0 +1,149 @@ +/* + * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +/* @test + * @bug 6607130 + * @summary Checks that JComboBox cell editor is hidden if the same + * item is selected with keyboard. + * Also checks that JComboBox cell editor is hidden if F2 and then + * ENTER were pressed. + * @author Mikhail Lapshin + */ + +import sun.awt.SunToolkit; + +import javax.swing.*; +import javax.swing.table.DefaultTableModel; +import java.awt.*; +import java.awt.event.KeyEvent; + +public class bug6607130 { + private JFrame frame; + private JComboBox cb; + private Robot robot; + + public static void main(String[] args) throws Exception { + final bug6607130 test = new bug6607130(); + try { + SwingUtilities.invokeAndWait(new Runnable() { + public void run() { + test.setupUI(); + } + }); + test.test(); + } finally { + if (test.frame != null) { + test.frame.dispose(); + } + } + } + + public bug6607130() throws AWTException { + robot = new Robot(); + } + + private void setupUI() { + frame = new JFrame(); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + DefaultTableModel model = new DefaultTableModel(1, 1); + JTable table = new JTable(model); + + cb = new JComboBox(new String[]{"one", "two", "three"}); + table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(cb)); + frame.add(table); + + frame.pack(); + frame.setLocationRelativeTo(null); + frame.setVisible(true); + } + + private void test() throws Exception { + realSync(); + test1(); + realSync(); + checkResult("First test"); + test2(); + realSync(); + checkResult("Second test"); + } + + private void test1() throws Exception { + // Select 'one' + hitKey(KeyEvent.VK_TAB); + realSync(); + hitKey(KeyEvent.VK_F2); + realSync(); + hitKey(KeyEvent.VK_DOWN); + realSync(); + hitKey(KeyEvent.VK_DOWN); + realSync(); + hitKey(KeyEvent.VK_ENTER); + realSync(); + + // Select 'one' again + hitKey(KeyEvent.VK_F2); + realSync(); + hitKey(KeyEvent.VK_DOWN); + realSync(); + hitKey(KeyEvent.VK_ENTER); + realSync(); + } + + private void test2() throws Exception { + // Press F2 and then press ENTER + // Editor should be shown and then closed + hitKey(KeyEvent.VK_F2); + realSync(); + hitKey(KeyEvent.VK_ENTER); + realSync(); + } + + private void checkResult(String testName) { + if (!cb.isShowing()) { + System.out.println(testName + " passed"); + } else { + System.out.println(testName + " failed"); + throw new RuntimeException("JComboBox is showing " + + "after item selection."); + } + } + + private static void realSync() { + ((SunToolkit) (Toolkit.getDefaultToolkit())).realSync(); + } + + public void hitKey(int keycode) { + robot.keyPress(keycode); + robot.keyRelease(keycode); + delay(); + } + + private void delay() { + try { + Thread.sleep(1000); + } catch(InterruptedException ie) { + ie.printStackTrace(); + } + } +} -- GitLab