How to remove a row in JTable via pressing on DELETE on the keyboard

前端 未结 3 1079
别跟我提以往
别跟我提以往 2020-12-21 17:14

I know that I can use KeyListener to check if DELETE (char) 127 is pressed or not, but how can I add keyListener to the selectedRow in JTable?

E

3条回答
  •  半阙折子戏
    2020-12-21 17:53

    You don't need to add one to the row. Just add one listener to the table and have it ask the table which row is selected.

    You can also try keyTyped instead of keyPressed. Some platforms have had issues where one works and the other doesn't.

    If you wanted to let users configure their key bindings you could as @hovercraft suggested and use key bindings. It requires mapping a KeyStroke to an action name with their InputMap and mapping the action names to Actions with their ActionMap.

    table.getInputMap().put(KeyStroke.getKeyStroke("DELETE"),
                            "deleteRow");
    table.getActionMap().put("deleteRow", yourAction);
    

提交回复
热议问题