简体   繁体   中英

Change ENTER Key function

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.

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));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM