Help to understand the following method?

0

I have the following method in my program, and although I understand it a bit I can not capture its logic completely. I am a newbie with the programming of Java.Swing and NetBeans and I am learning alone but apparently the method is a bit complex and / or advanced therefore I can not understand well the method is the following:

private void lastNametxtActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        InputMap im = lastNametxt.getInputMap(JComponent.WHEN_FOCUSED);
        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false), "ADD BUTON");
    ActionMap ap = lastNametxt.getActionMap();
    ap.put("ADD BUTON", new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            btnAdd.doClick();

            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

    });

}

What I intend to do with this is that pressing ENTER on a certain JTextField activates a certain button. It works but I do not understand it.

    
asked by Luis Consuegra 08.07.2018 в 06:06
source

1 answer

1

Greetings,

The code you share with us refers to a "technique" called Key Bindings which basically allows any component (that is extended by JComponent ) to respond to the input of keys pressed by the user.

Something very similar to this is the use of the KeyListener , although it seems easier set a KeyListener to set the Key Bindings (since at first glance it seems complex) the truth is that it is not like that and also the use of Key Bindings gives you many advantages. Everything is at your disposal which one to use.

Here I leave you a link to another answer on how to use Key Bindings (although it is in English it helped me a lot ).

Returning to the topic, basically the use of Key Bindings is done with two classes: InputMap and ActionMap .

The class InputMap allows us to "bind" (binding) or "relate" a key entry event with an Object. Whereas the class ActionMap allows us to relate these objects with actions that must be carried out.

The parameter that you entered when obtaining the InputMap of your object JTextField indicates when it should be "activated". Normally 3 conditions are used:

  • JComponent.WHEN_FOCUSED : When the component has the focus of the keyboard.
  • JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT : When the component contains (or is) the component that has the focus.
  • JComponent.WHEN_IN_FOCUSED_WINDOW : When your application has the focus (that is, when there is no other application focused) or, it contains the component that has the focus.

In your case, you indicated that it will be "activated" if your JTextField has the focus, that is, if another component has the keyboard focus, the actions will not be executed.

By doing this:

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false), "ADD BUTON");

You are indicating that a new relation will be added, the first parameter you entered ( KeyStroke ) is the key event, which tells us 3 things:

  • The key pressed (in your case it is the ENTER key).
  • Modifiers (if it must admit special keys like SHIFT, CTRL, ALT ...), if this remains in '0' it indicates that no modifier will be added.
  • If true indicates whether it should be activated when the key is released. Otherwise, it is activated when pressed.

And the second parameter is the object to which the relation will be made. It could be taken as an identifier (to know what the event will be called).

On the other hand, by doing this:

ap.put("ADD BUTON", new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent ae) {
        btnAdd.doClick();
    }
});

You are adding an action ( AbstractAction ) to that object ( ADD BUTON ). Where the action to execute is the one that is defined in the actionPerformed method.

Basically, with this we conclude that the Key Binding that you have set in your object JTextField will trigger an added button when you press the ENTER key when you have the focus on your object JTextField .

    
answered by 09.07.2018 / 08:27
source