I think the JTable component should better handle the keys that will start editing. I mean, with the current implementation, if you type DEL, Ctrl + Shift + DEL, F5, F7, for example, the cell editor appears in the edited cell. In my opinion, start the cell editor with keys that are not very intuitive for the end user.
There is also another problem: JTable is not aware of other possible key bindings defined in the form. If you have the Ctrl + Shift + C key binding defined for the button in your form, if you type this key combination in your JTable, the table will start editing, and after that the action with the key binding will be called. I think there should be an easy way to prevent this, and not turn off all those already defined key bindings in the keyword binding mapping.
Is there any third-party component that has already solved, at least in part, some of these problems, especially the one that starts editing with a reasonable key? I would not want to do all the tedious filtering.
Any help would be greatly appreciated. Thank.
Marcos
UPDATE
As long as I use this extremely imperfect "solution", it at least makes things worse at the moment. Improvements, comments and suggestions are welcome.
@Override
public boolean isCellEditable(EventObject e)
{
if (e instanceof MouseEvent)
{
return ((MouseEvent) e).getClickCount() >=
_delegate.getMouseClickCountToStartEditing();
}
else if (e instanceof KeyEvent)
{
KeyEvent event = (KeyEvent) e;
int key = event.getKeyCode();
if ((key >= KeyEvent.VK_F1 && key <= KeyEvent.VK_F12) &&
KeyStroke.getKeyStrokeForEvent(event) != _startEditingKey)
{
return false;
}
int ctrlAlt = KeyEvent.CTRL_DOWN_MASK | KeyEvent.ALT_DOWN_MASK;
if ((event.getModifiersEx() & ctrlAlt) == ctrlAlt)
{
return true;
}
if ((event.getModifiersEx() & ctrlAlt) != 0)
{
return false;
}
return true;
}
else
{
return true;
}
}
private KeyStroke getStartEditingKey()
{
InputMap bindings = TheTable.this.getInputMap(
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
for (KeyStroke key : bindings.allKeys())
{
Object binding = bindings.get(key);
if ("startEditing".equals(binding))
{
return KeyStroke.getKeyStroke(
key.getKeyCode(), key.getModifiers(), true);
}
}
return null;
}
source
share