views:

358

answers:

0

Hi,

I'm having a JTable containing JComboBox editors initialized somewhat like

JComboBox comboBox = ...;
TableColumn tc = table.getColumnModel().getColumn(i);
tc.setCellEditor(new DefaultCellEditor(comboBox));

This is working otherwise fine but I'd like to be able to navigate in the table and update the values with keyboard only. Now this is possible with the combo boxes but if I want to update the value "1" I must first press a key to activate the combo box and then press "1" to select the item.

So, what I want is that I could press "1" and the item would be selected with only one key press.

For the text cells I've managed to do this with prepareEditor like the following...

@Override
public Component prepareEditor(TableCellEditor editor, int row, int column) {
    Component c = super.prepareEditor(editor, row, column);
    if (c instanceof JTextComponent) {
        ((JTextComponent) c).selectAll();
    } 
    return c;
}

... but I haven't managed to figure out what to do with the combo box.

One possibility could be own TableCellEditor but if there's a more simple solution that would be nice =)

br, Touko