Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to add multiple ActionListeners to multiple Buttons

 
Dimuthu Lakmal
Greenhorn
Posts: 8
Android Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).

 
Dimuthu Lakmal
Greenhorn
Posts: 8
Android Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
Android Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 40
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By seeing the code I think actionListener is not added on noButton.
Also there is no handler on click of noButton

Ty adding following

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic