Rhs
Rhs

Reputation: 3318

How does the ActionListener work?

I found similar code to the following on Oracle's website. I stripped some irrelevant stuff regarding layout for space reasons.

private JTextField textField;

public class TextDemo extends JPanel implements ActionListener
{
    public TextDemo()
    {
        textField = new JTextField(5);
        //This causes a leaking this in constructor warning...
        textField.addActionListener(this);

        //code here for layout and to add the textfield to the panel
    }


    private static int ctr = 0;

    @Override
    public void actionPerformed(ActionEvent evt)
    {
        System.out.println(ctr++);           
    }

}

So I made a print statement to print and increment a counter to check when this actionListener is detecting an action.

I was wondering:

  1. Is the only action that triggers this method the enter button?
  2. In my constructor where I attached this to the action listener of the textField object, what exactly happens?

Upvotes: 2

Views: 2335

Answers (4)

Reimeus
Reimeus

Reputation: 159784

Is the only action that triggers this method the enter button?

Yes. For JTextFields an ActionEvent is dispatched by pressing ENTER.

In my constructor where I attached this to the action listener of the textField object, what exactly happens?

You register the ActionListener with the component. When an ActionEvent is triggered it dispatches an ActionEvent where actionPerformed is invoked, passing it the details of the source object in the ActionEvent.

The preferred approach for the implementation of ActionListeners is to use a separate anonymous listener per component or a single concrete Action for shared functionality.

Upvotes: 1

Raghunandan
Raghunandan

Reputation: 133560

An action event occurs, whenever an action is performed by the user.

Examples: When the user clicks a button, chooses a menu item, presses Enter in a text field. The result is that an actionPerformed message is sent to all action listeners that are registered on the relevant component.

this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this

 textField.addActionListener(this); // registering actionlistener

Capturing the action event

@Override
public void actionPerformed(ActionEvent evt)
{
    System.out.println(ctr++);   //perform some action on click        
}  

http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

As suggested by Hovercraft Full Of Eels you can also use annonymous inner class as below

 textField.addActionListener(new ActionListener() {
             @Override
              public void actionPerformed(ActionEvent arg0) {
              //do something
              }  
         });

Upvotes: 1

Lee Meador
Lee Meador

Reputation: 12985

First: Typically one uses a DocumentListener for a JTextField. It tells lots more interesting information about what is going on. It actually ties to the document (a sort of Model) that lies behind the GUI field.

Now:

Q1 - It's hard to figure out which mouse and keyboard actions cause the various events on the Swing screen components. As I remember, may be it for ActionPerformed. But there can be custom code added to a sub-class of a JTextField that causes an action event to fire for anything you want. You DO have to be careful if you do this.

Q2: The Listener object is stored in a list of all the objects that want to know when the text field has that action event occur. When it occurs, the text field calls the actionPerformed() method on all the objects in its listener list.

You might want to do some research on the Observer Pattern. That is the a name often used for bits of code that do this sort of thing. It can be used in many situations. The key is that it loosely couples the listener and the listenee (observer and observed). The object listening only has to tell the object to which it is listening that it want to be notified in certain cases. The object that is being listened-to keeps a list of all the various objects listening and what events they want to be informed of. That's all the connection and with the use of simple interfaces, it can be programmed simply.

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691755

  1. AFAIK, yes. The listener is called when enter is pressed while the textfield has the focus.
  2. The current object, being constructed, is added to the list of ActionListeners of the text field, so that when enter is pressed in the text field, the current object's actionPerformed() method is called. It's generally a bad practice to pass an object not being fully constructed to another object, because the other object could call it back directly and the object wouldn't be functional, since not fully constructed yet.

Upvotes: 1

Related Questions