3

I have defined cell editors for the two columns in my table in the following manner:

Java Code:

JComboBox combo = new JComboBox();
//code to add items to the combo box goes here.

JTextField textField = new JTextField();
textField.setHorizontalAlignment(JTextField.RIGHT);

TableColumn column = myJTable.getColumnModel().getColumn(0);
column.setCellEditor(new DefaultCellEditor(combo));

column = myJTable.getColumnModel().getColumn(1);
column.setCellEditor(new DefaultCellEditor(textField));

The problem I am facing is that when a focus is moved to a table cell, the cell doesn't become automatically editable. So, when the focus is moved to column 2 (that has a text field as an editor), the caret sign doesn't not appear unless the cell is double-clicked or the user starts typing. Similar is the case for column 1 (that has a combo box as an editor) as here the combo box doesn't appear unless the cell is clicked. These behaviors are counter-intuitive and undesirable for a user operating with the keyboard.:(

Please suggest pointers on how this could be resolved.

Thanks in advance.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
zerone
  • 105
  • 1
  • 3
  • 10
  • 1
    You can use `JComboBox` as a cell renderer as well. You can also utilize [DefaultCellEditor.setClickCountToStart](http://docs.oracle.com/javase/6/docs/api/javax/swing/DefaultCellEditor.html#setClickCountToStart%28int%29) to override default double click behavior. – tenorsax Aug 07 '12 at 02:07

2 Answers2

4
  1. This example overrides editCellAt() in a JTable having a DefaultCellEditor using JTextField.

  2. You can bind the Space key to the startEditing action defined for JTable:

    table.getInputMap().put(
        KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "startEditing");
    
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
3

As commented above, you can use JComboBox both as renderer and editor. Below is a very basic example. It also shows DefaultCellEditor.setClickCountToStart() usage.

import java.awt.Component;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

public class ComboBoxEditorDemo {

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("ComboBoxEditorDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTable table = new JTable(
                new Object[][] { { "1", "2" }, { "1", "2" } }, 
                new Object[] {"Col1", "Col2" });

        table.setRowHeight(24);

        TableColumn column = table.getColumnModel().getColumn(1);
        column.setCellRenderer(new MyComboBoxRenderer(new String[] { "1", "2", "3" }));
        column.setCellEditor(new MyComboBoxEditor(new String[] { "1", "2", "3" }));

        DefaultCellEditor editor = new DefaultCellEditor(new JTextField());
        editor.setClickCountToStart(1);
        column = table.getColumnModel().getColumn(0);
        column.setCellEditor(editor);

        JScrollPane scrollPane = new JScrollPane(table);
        frame.add(scrollPane);
        frame.pack();
        frame.setVisible(true);
    }

    static class MyComboBoxRenderer extends JComboBox implements
            TableCellRenderer {
        public MyComboBoxRenderer(String[] items) {
            super(items);
        }

        public Component getTableCellRendererComponent(JTable table,
                Object value, boolean isSelected, boolean hasFocus, int row,
                int column) {
            if (isSelected) {
                setForeground(table.getSelectionForeground());
                super.setBackground(table.getSelectionBackground());
            } else {
                setForeground(table.getForeground());
                setBackground(table.getBackground());
            }

            setSelectedItem(value);
            return this;
        }
    }

    static class MyComboBoxEditor extends DefaultCellEditor {
        public MyComboBoxEditor(String[] items) {
            super(new JComboBox(items));
        }
    }
}
tenorsax
  • 21,123
  • 9
  • 60
  • 107