1
JFrame myframe = new JFrame("My Sample Frame");
  JButton mybutton = new JButton("Okay");

Can someone explain to me these part.

 mybutton.addActionListener(new ActionListener(){

  public void actionPerformed(ActionEvent evt){

  //Assuming that the content here will do something.

  }
James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Kevin Florenz Daus
  • 597
  • 3
  • 9
  • 23

5 Answers5

5

What exactly do you not understand about the code?

The code adds an action listener to the button. The actionPerformed method of the action listener will be called when the button is clicked.

Note that an anonymous inner class is used here.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • Hi and Thank you Jesper. Im not used to anonymous inner class , and in fact I just knew it just by now. .Thank you and more powers. one last question is these practice good or bad , I mean the way I set the event listener. – Kevin Florenz Daus Jun 14 '11 at 13:46
  • 1
    This is not bad practice, in fact setting event handlers is commonly done with anonymous inner classes just like in the example you showed. – Jesper Jun 14 '11 at 14:36
  • Hi Jesper, in following Line {mybutton.addActionListener(new ActionListener()} which is part of the original question I am struggling with 'new ActionListener()' placed in brackets It would be great if you could explain it. Thanks. – Deepak Apr 13 '18 at 12:17
  • @Deepak This is Java's syntax to create an anonymous inner class. It creates a subclass of `ActionListener` on the spot, and between the `{ ... }` you can override or implement methods, such as `actionPerformed` in the example above. – Jesper Apr 16 '18 at 06:31
5

Anonymous Inner Class is used here.

You have technically implemented ActionListener. When you called addActionListener:

mybutton.addActionListener(new ActionListener(){

 public void actionPerformed(ActionEvent evt){

    //Assuming that the content here will do something.

 }

You created an instance of an anonymous class, or a class that implements ActionListener without a name.

For the same please visit this link .

Community
  • 1
  • 1
Suhail Gupta
  • 22,386
  • 64
  • 200
  • 328
4

You should read this Tutorial about writing Event Listeners.

oliholz
  • 7,447
  • 2
  • 43
  • 82
  • +1, although you didn't really "explain" anything...you just referred OP somewhere else. – mre Jun 14 '11 at 13:39
2

In order to have a button react to events (such as a click) it must have an ActionListener.

In the code you posted, you are creating an anonymous class implementing ActionListener

public void mySetupFunction(){

    mybutton.addActionListener(new ActionListener(){

    public void actionPerformed(ActionEvent evt){
        //Do something when the button is clicked
    });
}

is the same as doing :

public void mySetupFunction(){

    mybutton.addActionListener(new MyEventHandler());
}

with :

public class MyEventHandler implements ActionListener{
    public void actionPerformed(ActionEvent evt){
        //Do something when the button is clicked
    }
}
alain.janinm
  • 19,951
  • 10
  • 65
  • 112
Jason S
  • 137
  • 2
  • 9
0

If it is safe to assume, you are either new to Event handling or new to Java either way I have generalised the answer to accomodate both cases :)

Expert Answer:ActionListener

There isn't one, it's really self led and reading the APIs

Simple Answer:A sugar coated explanation of what You provided in your code.

mybutton

is a reference to the instanciated Object JButton

xxxxxx.addActionListener()

is a function call to a Field inherited from class javax.swing.AbstractButton

JButton inherits AbstractButton fields and therefore takes us to the next explanation

new ActionListener(){}

ActionListener is an interface, we don't need to go this high up at all.. all we need to know is it listens for input to source from a given object

public void actionPerformed(ActionEvent e)

again this is a function call, it is the tip to our "Objects pyramid" and not atypically - will contain the response to what you want the base of your object to do .. in this case mybutton.

What you do from here is dependent on what you want actionPerformed() to do.

The question to ask yourself if this is part of an assignment is - What does this do? How does it Do it?