Search code examples
javaswingbindingkey-bindings

Key Bindings as Alternative to KeyEvent still nonfunctional


Having tried KeyEvents it was recommended that I switch to Key Bindings to activate certain events by the pushing of the arrow keys while one is in a TextArea

                        area.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
                    .put(KeyStroke.getKeyStroke("VK_UP"), 
                            "doEnterAction");
                    area.getActionMap().put("doEnterAction", new AbstractAction(){
                        @Override
                        public void actionPerformed(ActionEvent e){
                            System.out.println("Event Handled");
                            oneRay[pick][0] = ("");
                             if(i>=4){
                                 i=0;
                                 area.setText("");
                             }
                            caller();
                        }
                    });

                    area.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
                    .put(KeyStroke.getKeyStroke("VK_DOWN"), 
                            "doEnterAction");
                    area.getActionMap().put("doEnterAction", new AbstractAction(){
                        @Override
                        public void actionPerformed(ActionEvent e){
                            System.out.println("Event 2 Handled");
                            area.append("\n"+oneRay[pick][1]);
                             buton1.setEnabled(true);
                             buton2.setEnabled(true);
                        }
                    });

                    area.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
                    .put(KeyStroke.getKeyStroke("VK_RIGHT"), 
                            "doEnterAction");
                    area.getActionMap().put("doEnterAction", new AbstractAction(){
                        @Override
                        public void actionPerformed(ActionEvent e){
                             if(i>=4){
                                 i=0;
                                 area.setText("");
                             }
                             caller();
                        }

This code covers three different Key Bindings, but none work, whether I press up down left right the cursor just moves in that direction in the TextArea. What have I done wrong this time. Please help me!


Solution

  • whether I press up down left right the cursor just moves in that direction in the TextArea.

    You are building the KeyStroke incorrectly. You should not be including the "VK_" in the Keystroke. So basically nothing is being added to the InputMap.

    Also your code is updating the InputMap and ActionMap with a new identifier. I find is easier to just replace the Action in the ActionMap. See Key Bindings for a list of all the default Actions as well as the basic code for replacing the default Action (this is a different link than you got in your last posting).

    Finally, in your other posting you suggested that you want to invoke the Action of a button. Well then your code should be creating an Action that can be used by the button and the Key Bindings. You create an Action the same way you create an ActionListener except you extend AbstractAction instead of implementing ActionListener.