How to use multiple actionlisteners
Hi,
I am trying to make a code that has multiple buttons, but the only way i know how to make action listeners are to either use inner classes (which require variable to be final, which isnt good for me), or to use AddActionListener(this), which only allows for one action listener.
Ive heard somehting about getSource(), but cant find any good examples of how its used... Are there any other ways to use multiple action listeners?
This is a code i was experimenting with getSource on, but i kept on getting an error:
Code :
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class FindAddPage implements ActionListener
{
public static void main (String[] args)
{
int counter = 0;
//set up frame
GridLayout myLayout = new GridLayout (2,1);
JFrame myFrame = new JFrame ("Add/Find");
myFrame.setSize(400,200);
JPanel myPanel = new JPanel();
myPanel.setLayout(myLayout);
//make buttons
JButton add = new JButton ("Add A Tutor");
JButton find = new JButton ("Find A Tutor");
if(event.getSource()==add){
counter++;
}
myFrame.setVisible(true);
}
}
Re: How to use multiple actionlisteners
You can also make an inner class that extends ActionListener, create an instance of that and add it as a listener.
Does your usage of getSource() work? Where is the object: event defined and given a value?
Do a search on the forum for getSource() for some code samples.
Re: How to use multiple actionlisteners
May be if our forum Vip means this try it
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==add){
counter++;}
}
Re: How to use multiple actionlisteners
What does event.getSource() returns???
Why don't you try casting it?
Re: How to use multiple actionlisteners
Hi,
I tried using this:
Code :
ActionListener al = new ActionListener(){
public void actionPerformed(ActionEvent event){
if(event.getSource()==add){
counter++;
}
}
};
However it still gave me the error that "add" and "counter" would need to be made final... Is there a way to get around this?
Re: How to use multiple actionlisteners
try to make add and counter Class variable
Re: How to use multiple actionlisteners
Well, it's saying you coz i guess you are drawing buttons in your main() method, am i right?
If so, remember, main() is a static method and you can't reference the object from static context like that.
Well, can you give your whole code? Or is that all?
Re: How to use multiple actionlisteners
I will suggest you some things.
1. Draw the GUI in constructor.
2. And then add an actionevent to the button, you want to.
3. Make an object of GUI class in your main and set it to visible.
Re: How to use multiple actionlisteners
Move the code out of the main method into the constructor for the class and have the only code in the main method call that constructor. Then you can use class variables.
Re: How to use multiple actionlisteners
Hey,
I got it to work. I made the GUI in a constructor method and then used main to create the new GUI. Thank You for the help :)