This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes How to add multiple ActionListeners to multiple Buttons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to add multiple ActionListeners to multiple Buttons" Watch "How to add multiple ActionListeners to multiple Buttons" New topic
Author

How to add multiple ActionListeners to multiple Buttons

Dimuthu Lakmal
Greenhorn

Joined: Nov 03, 2012
Posts: 8

I'm trying to figure out how to add multiple ActionListeners to multiple JButtons using one class without including inner classes to JButtons.
I tried to create a basic program using following codes to get the result. But it didn't give the proper result.
Can you show me the fault of this program?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class MyFrame extends JFrame implements ActionListener{
private JButton yesButton;
private JButton noButton;
private JButton cancelButton;
private JButton exitButton;
private JTextField txtfd;

MyFrame(){
setSize(300,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new FlowLayout());

txtfd=new JTextField(10);

add(txtfd);

yesButton=new JButton("Yes");
yesButton.addActionListener(this);
add(yesButton);
noButton=new JButton("No");
add(noButton);

pack();

}
public void actionPerformed(ActionEvent e){

if(e.getSource()==yesButton){
String text=yesButton.getText();
txtfd.setText(txtfd.getText()+text);
}
if(e.getSource()==cancelButton){
String text=cancelButton.getText();
txtfd.setText(txtfd.getText()+text);
}

}
}

class F1{
public static void main(String args[]){
new MyFrame().setVisible(true);
}
}
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Dimuthu Lakmal wrote:I'm trying to figure out how to add multiple ActionListeners to multiple JButtons using one class without including inner classes

Why the arbitrary restrictions? Why just one class? Why not inner classes? How about anonymous inner classes?

to JButtons.
I tried to create a basic program using following codes to get the result. But it didn't give the proper result.
Can you show me the fault of this program?


That program has one ActionListener, two JButtons, and only one with the ActionListener set. So it doesn't really reflect the questions you are asking. You also don't tell us what it does wrong (what 'didn't give the proper result' means). So it is hard to tell you what to change if we don't know what you expect and what the difference is. Can you provide more information please?

Also, below is your code reposted with code tags. Please read UseCodeTags (<- click).



Steve
Dimuthu Lakmal
Greenhorn

Joined: Nov 03, 2012
Posts: 8

Steve Luke wrote:
Dimuthu Lakmal wrote:I'm trying to figure out how to add multiple ActionListeners to multiple JButtons using one class without including inner classes

Why the arbitrary restrictions? Why just one class? Why not inner classes? How about anonymous inner classes?

That program has one ActionListener, two JButtons, and only one with the ActionListener set. So it doesn't really reflect the questions you are asking. You also don't tell us what it does wrong (what 'didn't give the proper result' means). So it is hard to tell you what to change if we don't know what you expect and what the difference is. Can you provide more information please?


I want to display "yesno" in the text field when we click noButton after clicking yesButton. But when i click these both button one after other, it displays only "yes" text. It seems the second if statement does't working properly. What's wrong with this program?

Also, below is your code reposted with code tags. Please read UseCodeTags (<- click).

Thanks for the clue

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

You know how to get the "yes" displayed in the text box. You know this works because you do something to the yes button, so that when it is pushed some work is done. What do you think you have to do to get the same thing to happen when you press the no button?
Dimuthu Lakmal
Greenhorn

Joined: Nov 03, 2012
Posts: 8

Steve Luke wrote: What do you think you have to do to get the same thing to happen when you press the no button?

I tried it by using another if statement. But it doesn't give the related result ( printing "no" in text field). I have no idea....
Vijay Vishwa
Greenhorn

Joined: Nov 21, 2010
Posts: 28
By seeing the code I think actionListener is not added on noButton.
Also there is no handler on click of noButton

Ty adding following



SCJP 5.0 OCBCD 5.0(Formerly SCBCD)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to add multiple ActionListeners to multiple Buttons