Programmatically enable editing a JTable cell on keystroke

时光毁灭记忆、已成空白 提交于 2019-12-01 13:47:33

F2 already is the default KeyStroke used by JTable to start editing.

See Key Bindings for a table of all the KeyStrokes used by all the components. You will also find examples of using key bindings.

If you do create you own Action, instead of using the provide Action then the code should be something like:

int row = table.getSelectedRow();
int column = table.getSelectedColumn();

if (editCellAt(row, column))
{
    Component editor = table.getEditorComponent();
    editor.requestFocusInWindow();
}

So the editor gets focus once the key is pressed.

Apparently, Aqua LAF doesn't bind F2 so it looks like you need to do it yourself. Assuming the "startEditing" Action is defined in the ActionMap you can use:

KeyStroke keyStroke = KeyStroke.getKeyStroke("F2");
InputMap im = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
im.put(keystroke, "startEditing");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!