hi all
im janaka priyadarsahana

i want to write actions for buttons.
i have 4 buttons and when they click i want to do four seperate things

for this i have several options

  1. create new class by implimenting Action listener interface and using an object of that class, can handle the actions.

  2. use one class by implimenting Action listener interface and create all buttons in that class and then add actions for the buttons

  3. create 4 seperate instence of action listener interface and do required things.

this is as follow

public class MyClass{

JButton myButton1 = new JButton();

ActionListener action1 = new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                        //do required

            }
    }

myButton1.addActionListener(action1)

}

i want to know what is the best method for my requirement

please anyone can help me..

thank you
janaka

Recommended Answers

All 6 Replies

This is how I tend to declare buttons with one off action events:

JButton btnFoo= new JButton("btnFoo");
    panel.add(btnFoo);
    btnFoo.addActionListener(
        new ActionListener(){
        public void actionPerformed(ActionEvent e){
                //code goes here
               }
        }
    );

For quit buttons and the like, I'll define (say) JCloseButton which extends JButton with a default listener.

This is how I tend to declare buttons with one off action events:

JButton btnFoo= new JButton("btnFoo");
    panel.add(btnFoo);
    btnFoo.addActionListener(
        new ActionListener(){
        public void actionPerformed(ActionEvent e){
                //code goes here
               }
        }
    );

For quit buttons and the like, I'll define (say) JCloseButton which extends JButton with a default listener.

thank you for your reply
but in your case it have to use the same code segment for each and every buttons. is that affected for the efficiency of the programe.

because i want to increase the efficiency of my program.

thank you

For quit buttons and the like, I'll define (say) JCloseButton which extends JButton with a default listener.

IE: Define a class which extends JButton, the first line of code calls JButton, then the rest of the code adds and defines an actionListener. Write tha action listener once (in the newly defined class) and use that class instead of JButton.

let me .......
the best way to do that would be..like this

public class MyClass
{
//-----first create 3 buttons-----
JButton myButton1 = new JButton();
myButton1.addActionListener(new listener1());
JButton yourButton=new JButton();
yourButton.addActionListener(new listener2());
JButton otherButton=new JButton();
otherrbutton.addActionlistener(new listener3());

class listener1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//---things to be done when myButton is clicked
}
}

class listener2 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//---things to be done when otherButton is clicked
}
}

class listener3 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//---things to be done when otherButton is clicked
}
}

}

i m sure this one u needed....................

commented: Resurrecting a two year old thread and on top of that suggesting a sub-standard solution -1

Look at the Java Sun tutorial on ActionEvents and ActionListener. They have all the examples you need as well. I was able to learn all about them by myself, and so will you, if you read. If you have any specific questions, ask those...

Look at the Java Sun tutorial on ActionEvents and ActionListener. They have all the examples you need as well. I was able to learn all about them by myself, and so will you, if you read. If you have any specific questions, ask those...

I am pretty sure the thread starter has already found a solution, sumit_hotchap is the culprit for resurrecting this thread of more than two years go.
<EDIT>
And just for the record, the first solution is the best solution, the thread starter just doesn't know what the heck she is talking about in the second post

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.