protected void addBindings() {
        InputMap inputMap = this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        ActionMap actionMap = this.getRootPane().getActionMap();

        KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_Z, Event.CTRL_MASK);
        inputMap.put(key, "undo");
        actionMap.put("undo", new UndoAction());


        key = KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK);
        inputMap.put(key, "completed");
        actionMap.put("completed", new CompletedAction());


        key = KeyStroke.getKeyStroke(KeyEvent.VK_D, Event.CTRL_MASK);
        inputMap.put(key, "delete");
        actionMap.put("delete", new DeleteAction());

    }

    class UndoAction extends AbstractAction {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("*****EXECMD: UNDO*******");
            JIDLogic.setCommand("UNDO");
            Task[] task = JIDLogic.executeCommand("UNDO");
            if(task == null)
                showPopup("UNDO unsuccessfully!");
            else {
                showPopup("UNDO: "+task[0].getName());
                expandJPanel.updateJTable();
            }
        }
    }

    class DeleteAction extends AbstractAction {
        @Override
        public void actionPerformed(ActionEvent e) {
            Task[] taskList = expandJPanel.getTasks();

            JIDLogic.setCommand("DELETE");
            Task[] task = JIDLogic.executeCommand("DELETE " 
                    + taskList[expandJPanel.jTable1.getSelectedRow()].getTaskId());

            if(task == null)
                MainJFrame.showPopup("DELETE unsuccessfully!");
            else {
                MainJFrame.showPopup("DELETE: "+task[0].getName());
                expandJPanel.updateJTable();
            }           
        }
    }

    class CompletedAction extends AbstractAction {
        @Override
        public void actionPerformed(ActionEvent e) {
            Task[] taskList = expandJPanel.getTasks();

            JIDLogic.setCommand("COMPLETED");
            System.out.println("COMPLETED "
                    + taskList[expandJPanel.jTable1.getSelectedRow()].getTaskId());
            Task[] task = JIDLogic.executeCommand("COMPLETED " 
                    + taskList[expandJPanel.jTable1.getSelectedRow()].getTaskId());

            if(task == null)
                MainJFrame.showPopup("COMPLETED unsuccessfully!");
            else {
                MainJFrame.showPopup("COMPLETED: "+task[0].getName());
                expandJPanel.updateJTable();
            }           
        }

This whole code is inside my class which extends JFrame.
The first and the third function can work but the second one cannot work.

There was no line printing after I pressed Ctrl+X. I have also tried chaging to other keys. But it still has problems.

Edited 4 Years Ago by jwings: putting wrong comment

So, is it because I have an editable JTextBox?

How to solve this problem?

Moreover, is there anyway to add global hotkey? I have tried JIntelligenttype API... but it has problems with windows 64-bit :\

If you want to add new hotkeys, regardless of any implementation issues, it would be better U.I. to avoid the commonly-used and known ones such as ctrl-X,C,V (and Z - undo - as well). Ypou should be on safe ground if you use different keys.
By "global" do yu mean active regardless of where your application focus is, or active when the focus is not in any window of your application?

This article has been dead for over six months. Start a new discussion instead.