JDK-6559152 : editable JComboBox: Can't select with keys inside JTable
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2007-05-18
  • Updated: 2011-03-07
  • Resolved: 2011-03-07
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6 JDK 7
6u4Fixed 7 b15Fixed
Related Reports
Duplicate :  
Relates :  
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
1.6.0_01

ADDITIONAL OS VERSION INFORMATION :
Linux (Suse 10.1), Windows XP

A DESCRIPTION OF THE PROBLEM :
Using java 1.6:
- A JComboBox as a CellEditor in a JTable
- The JComboBox is editable
- The items in the ComboBox are not Strings
I can't select an entry from the list using the cursor keys and return (the mouse works).

This worked in java 1.4 and 1.5 .

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Start the test program. Click into the Table. Use the cursor keys to select "2". Hit enter.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The "2" should be selected.
ACTUAL -
The "1" is selected.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
package test;

import java.util.Vector;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Test extends JFrame {

	public Test() {
		BoxItem item1 = new BoxItem("1");
		BoxItem item2 = new BoxItem("2");
		
		DefaultTableModel model = new DefaultTableModel(new Object[][] {{item1}}, new String[] {"test"});
		JTable table = new JTable(model);
		
		Vector v = new Vector();
		v.add(item1);
		v.add(item2);
		
		JComboBox box = new JComboBox(v);
		box.setEditable(true);
		table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(box));
		
		getContentPane().add(box);
		pack();
	}


	static class BoxItem {
		
		String s = null;
		
		public BoxItem(String s) {
			this.s = s;
		}
		
		public String toString() {
			return s;
		}
	}
	
	public static void main(String[] args) {
		Test t = new Test();
		t.setVisible(true);
	}
	
}

---------- END SOURCE ----------
It is copied from the description of 6557820:
FULL PRODUCT VERSION :
java version "1.6.0"
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
Microsoft Windows 2000 [Version 5.00.2195]


A DESCRIPTION OF THE PROBLEM :
In an editable combo box inside a JTable, when using the keyboard, presing enter or the space bar doesn't select the highlighted item. This worked with JDK 1.5.0.08.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Select the editable combo box
click esc to open the drop down list
using the arrow keys highlith one of the items
press enter (or space)

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The highlighted item should be selected
ACTUAL -
with enter the list is closed and nothing is selected, with the spacebar nothing happens.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import javax.swing.DefaultCellEditor;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
/*
 * Test.java
 *
 * Created on May 3, 2007, 1:29 PM
 */

/**
 *
 * @author  helm-youngs
 */
public class Test extends javax.swing.JFrame {
    
    /** Creates new form Test */
    public Test() {
        initComponents();
        getContentPane().add(scrollPane);
        pack();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        editableComboBox = new javax.swing.JComboBox();
        scrollPane = new javax.swing.JScrollPane();
        testTable = new javax.swing.JTable();
        
        editableComboBox.setEditable(true);
        editableComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Test");
        testTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
        },
            new String [] {
            "Title 1", "Title 2", "Title 3", "Title 4"
        }
        ));
        TableColumnModel columnModel = testTable.getColumnModel();
        TableColumn tableColumn = columnModel.getColumn(0);
        tableColumn.setCellEditor(new DefaultCellEditor(editableComboBox));
        scrollPane.setViewportView(testTable);        
    }//GEN-END:initComponents
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Test().setVisible(true);
            }
        });
    }
    
// Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox editableComboBox;
    private javax.swing.JScrollPane scrollPane;
    private javax.swing.JTable testTable;
// End of variables declaration//GEN-END:variables
    
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
using alt+up or down arrow selects the highlited item.

Release Regression From : 5.0u11
The above release value was the last known release where this 
bug was not reproducible. Since then there has been a regression.

Comments
SUGGESTED FIX Webrev: http://javaweb.sfbay/jcg/7/swing/6559152/ The fix is very simple: +++ BasicComboBoxUI.java Thu Jun 7 12:23:26 2007 @@ -1504,11 +1504,12 @@ else if (key == ENTER) { if (comboBox.isPopupVisible()) { // Forces the selection of the list item boolean isEnterSelectablePopup = UIManager.getBoolean("ComboBox.isEnterSelectablePopup"); - if (!comboBox.isEditable() || isEnterSelectablePopup) { + if (!comboBox.isEditable() || isEnterSelectablePopup + || 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
13-06-2007

WORK AROUND 1. You can set the "ComboBox.isEnterSelectablePopup" LaF property to TRUE. But this will affect all combo boxes in an applicatoin. 2. You can make the combo box uneditable. There is a workaround example code: ========== Code Begin =============== // Without any changes you will see that the ENTER doesn't work. // Try workarounds in the code to fix the problem. import javax.swing.*; import javax.swing.table.DefaultTableModel; public class WorkaroundExample extends JFrame { public static void main(String[] args) throws Exception { SwingUtilities.invokeAndWait(new Runnable() { public void run() { // Workaround 1 // Uncomment the following line // UIManager.put("ComboBox.isEnterSelectablePopup", Boolean.TRUE); WorkaroundExample t = new WorkaroundExample(); t.setVisible(true); } }); } public WorkaroundExample() { setDefaultCloseOperation(EXIT_ON_CLOSE); DefaultTableModel model = new DefaultTableModel(1, 1); JTable table = new JTable(model); JComboBox box = new JComboBox(new String[]{"one", "two", "three"}); // Workaround 2 - make a combo box uneditable // If the combo box is uneditable, the effect isn't shown // Replace "true" in the following line by "false" box.setEditable(true); table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(box)); add(box); pack(); setLocationRelativeTo(null); } } ========== Code End ===============
06-06-2007

EVALUATION First of all, the described effect can be reproduced with String objects too. Formally, it is a regression of 6428549. A couple of reasons caused such strange effect: - Look at the 4165987. There was added "lightweight" keyboard navigation for combo boxes. It means, that when a combo box is a cell editor and if you keyboard navigate through a combo box's items, the combo box won't commit each keypress as a change of the selected item. - Look at the 6428549. In the bounds of the fix there was added "ComboBox.isEnterSelectablePopup" LaF property. It determines combo box behavior in case of ENTER pressing when the popup is shown. By default the property is FALSE, i.e. if combo box is editable, ENTER pressing doesn't select a new item from popup. Workarounds 1. You can set the "ComboBox.isEnterSelectablePopup" LaF property to TRUE. But this will affect all combo boxes in an applicatoin. 2. You can make the combo box uneditable. There is a workaround example code: ========== Code Begin =============== // Without any changes you will see that the ENTER doesn't work. // Try workarounds in the code to fix the problem. import javax.swing.*; import javax.swing.table.DefaultTableModel; public class WorkaroundExample extends JFrame { public static void main(String[] args) throws Exception { SwingUtilities.invokeAndWait(new Runnable() { public void run() { // Workaround 1 // Uncomment the following line // UIManager.put("ComboBox.isEnterSelectablePopup", Boolean.TRUE); WorkaroundExample t = new WorkaroundExample(); t.setVisible(true); } }); } public WorkaroundExample() { setDefaultCloseOperation(EXIT_ON_CLOSE); DefaultTableModel model = new DefaultTableModel(1, 1); JTable table = new JTable(model); JComboBox box = new JComboBox(new String[]{"one", "two", "three"}); // Workaround 2 - make a combo box uneditable // If the combo box is uneditable, the effect isn't shown // Replace "true" in the following line by "false" box.setEditable(true); table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(box)); add(box); pack(); setLocationRelativeTo(null); } } ========== Code End ===============
06-06-2007