JDK-8032878 : Editable combos in table do not behave as expected
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 7u21
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2014-01-28
  • Updated: 2015-05-05
  • Resolved: 2014-03-21
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 7 JDK 8 JDK 9
7u72Fixed 8u20Fixed 9 b08Fixed
Related Reports
Duplicate :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
 
 Problem with Java version 7u21.
 
 Run the following test app, which uses standard DefaultCellEditors in a 
simple JTable.

 With the mouse: click in the cells of the first two columns.
  - Notice from the console output that the cell editor components gain focus 
(the caret is also visible) - The user can simply type in the new values.
  - The user can interact with the editor just as if they they are using a 
regular component.

 Now try with the keyboard: navigate to a cell. Start typing a new value.
  - The editor appears
  - The editor is not given focus and no caret is visible. The focus is still 
on the table!
  - When typing regular letters the value in the editor is updated, but only 
for the text field and not with the combo.
  - The combo's drop arrow is visible, but it cannot be dropped. 

  Test case:
import java.awt.FlowLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.DefaultCellEditor;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.JTextField;

public class ComboInTable {

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setSize(300, 200);
        f.setLocationRelativeTo(null);
        f.setLayout(new FlowLayout());
        f.add(createTable());
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

    private static JTable createTable() {
        JTable table = new JTable(new String[][]{{"One", "Red"},
                    {"Two", "Green"},
                    {"Three", "Yellow"}},
                new String[]{"#", "Colour"});

        JTextField tf = new JTextField();
        DefaultCellEditor tfEditor = new DefaultCellEditor(tf);
        tfEditor.setClickCountToStart(1);

        JComboBox combo = new JComboBox(new String[]{"Red", "Green", "Blue", 
"Yellow"});
        combo.setEditable(true);
        DefaultCellEditor comboEditor = new DefaultCellEditor(combo);
        comboEditor.setClickCountToStart(1);

        table.getColumnModel().getColumn(0).setCellEditor(tfEditor);
        table.getColumnModel().getColumn(1).setCellEditor(comboEditor);

        FocusL focusL = new FocusL();
        table.addFocusListener(focusL);
        tf.addFocusListener(focusL);
        combo.addFocusListener(focusL);
        combo.getEditor().getEditorComponent().addFocusListener(focusL);

        return table;
    }

    private static class FocusL extends FocusAdapter {
        public void focusLost(FocusEvent e) {
            System.out.println("Focus Lost: " + 
e.getSource().getClass().getName());
        }

        public void focusGained(FocusEvent e) {
            System.out.println("Focus Gained: " + 
e.getSource().getClass().getName());
        }
    }
}

Comments
SQE is ok to take the fix in PSU 14_04.
2014-05-29

JTable forwards key actions to the associated cell editor via InputMap with WHEN_FOCUSED condition. This works with JTextField but it doesn't with JComboBox because JTextField is ���hidden��� in combo box. The fix: To copy JTable action forwarding inside JComboBox so that it would forward actions to its editor, usually JTextField, when it's table cell editor.
2014-03-14