Query regarding use of ActionListener in java
Hi.
I am getting confused about the use of ActionListeners in the java program.
If I take multiple listeners in one program, is it valid?
The I using one listener for each button,only if each button is in it's own inner class.
In another program I'm using around 15 or more listeners in it. Is it anything invalid with programmer OR it is ok to use only one ActionListener in the program?
Please suggest me..
Re: Query regarding use of ActionListener in java
I'm not confirmed about behavior of java, if you add a single object but multiple times to the same object. But I know this is completely unnecessary. Suppose if you have two different buttons, and if you want your program to listen to both buttons. For this you have to add your main program to the ActionListeners list of each button you have taken.
Please make sure that your main program is not duplicating the actionsPerformed by two buttons(which you have defined) listening to each other.I hope this will solve your query.
Re: Query regarding use of ActionListener in java
Your method look harder to adapt ,should you wish to listen to the same command coming from different places.
According to my Java skill ,creating a general listener makes it easier to take advantages of inheritance in java programming.
For reference see below example:
//Base class
public class AppFrm extends JFrame implements ActionListener{
public void actionPerformed(ActionEvent e){
if ( e.getActionCommand().equals("Something") ){
doSomething();
}else if ( e.getActionCommand().equals("Else") ){
doElse();
}
}
}
//Inherited Class
public class NextFrame extends AppFrm implements ActionListener{
public void actionPerformed(ActionEvent e){
if ( e.getActionCommand().equals("Something") ){
doSomthingElse();
super.actionPerformed(e);
}else{
super.actionPerformed(e);
}
}
}
Re: Query regarding use of ActionListener in java
In java ActionListener interface is used to receive events.The ActionListener interface extends EventListerner
The class which is interested in processing an action event implements this interface, and the object created with that class is registered with a component, using the component's addActionListener method.
actionPerformed method is invoked only when the event occurs.
Syntax for this is:
1. actionPerformed(ActionEvent e)
2.public void actionPerformed(ActionEvent e)