Change behavior of JTable key actions

后端 未结 2 1177
忘掉有多难
忘掉有多难 2020-12-21 17:45

I have a JTable with editable cells. When I click in a cell, it enters edit mode; the same happens when I\'m moving through cell using the directional arrows. N

相关标签:
2条回答
  • 2020-12-21 18:10

    Use Key Bindings for this. Most Look & Feel implementations already bind F2 to the table's startEditing action, but you add a different binding:

    tree.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "startEditing");
    

    This will effectively replace the previous binding of Enter to the table's selectNextRowCell action.

    0 讨论(0)
  • 2020-12-21 18:28

    Here is what i would do:

    • First enable the single cell selection for the JTable
    • Create a KeyAdapter or KeyListener for the JTable or for the JPanel, what contains your table.
    • In the KeyAdapter's keyPressed() method enter the edit mode of the selected cell, something like this: http://www.exampledepot.com/egs/javax.swing.table/StopEdit.html

    You can check in the keyPressed() method, if the user pressed the right button for editing. I'm not sure, if the normal (double click) editing is disabled in your table, then what happens, if you try to edit it programmatically, but if it doesn't work, then you can enable the editing on the selected cell, when the user presses the edit button, then when he/she finished, disable it again.

    0 讨论(0)
提交回复
热议问题