Change ENTER Key function

佐手、 提交于 2019-12-02 10:56:08

问题


I wanted to change the default action of the ENTER key on JTable, so I used this code:

table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
        .put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Enter");
table.getActionMap().put("Enter", new AbstractAction() {
    private static final long serialVersionUID = 1L;
    public void actionPerformed(ActionEvent ae) {
        //my action
    }
}

Tt works normally. What I want now is to change the row just after my action. In other words, execute the default action of the enter key.


回答1:


The default Action for the ENTER key is "selectNextRowCell". As shown here, you can obtain a reference to the original Action and evoke in your new handler.

String name = "selectNextRowCell";
Action action = table.getActionMap().get(name);
…
public void actionPerformed(ActionEvent ae) {
    action.actionPerformed(new ActionEvent(table, ActionEvent.ACTION_FIRST, name));
}


来源:https://stackoverflow.com/questions/20592523/change-enter-key-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!