Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8032878

Editable combos in table do not behave as expected

    XMLWordPrintable

Details

    Backports

      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());
                }
            }
        }

        Attachments

          Issue Links

            Activity

              People

                aivanov Alexey Ivanov
                asaha Abhijit Saha
                Votes:
                0 Vote for this issue
                Watchers:
                5 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: