Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to add an action litener to another class 1

Status
Not open for further replies.

stephenbruceburch

Technical User
Jun 1, 2002
11
0
0
US
I made a program that has a keypad and a few other buttons. I got the program to work but it looked kindof nasty all in one class so i decided to spit it up. so now I have a keypad class, and my main class, I also have a few other classes but they're working. What I want to do is add the action listener on the keypad buttons to the mainclass. I have made the buttons public and added them as action listeners in the mainclass but i would like to do it in the keypad class so my main class is not all clutered. Can anyone help? Thanks
 
you can add an action listener as an anonymous inner class, or just as an inner class, or implement a listener or adapter class.

An anonymous inner class is one that is coded in line: when you add the action listener to your button you add it as a new ActionListener there and define the methods you need in accompanying brackets e.g.

your button.addActionListener(
new Action Listener(){
public void blahblah(ActionEvent e)
{
//your code;
}
}
);

You will list all the required methods from the Action Listener class within these brackets. The compiler spots these and creates a separate class file for them as a result.

An inner class is just defining the actionlistener (or any other class) within the same source file as your main public class, but it can't be coded as a public class itself (you can only define one public class within a java file). Sometimes it is easier to code a listener class this way because anyone reading your code will know to associate the class with the public class in the same source file.

Another (and I find less confusing) is to make your main class implement a listener or adapter class. This way you code the methods used with the listener/adapter class along with the definition of methods of your main class:

public class Myclass extends JFrame implements ActionListener, MouseMotionListener
{
//define variables
public method1()
{
//method 1 code
}

private void method2()
{
//method2 code
}
public void actionPerformed(ActionEvent e)
{
//define your action event code
}
public void mouseMoved(MouseEvent e)
{
//define your mousemoved code here
}
public void mouseDragged(MouseEvent e){}
}

...Note that by doing it this way, whenever you add the listener to your component, you should always add it using the this keyword (because it is this class that will act as the listener). Using this latter methid will mean the inclusion of some methods even if you don't intend to use them - they must be there for the compiler otherwise it trows a wobbler. As above, implementing the mousemotionlistener class, you must code the interface for the mouseDragged event even though you may not want to use it (hence why it is blank).

Here are just three ways, I am sure there are more but they are for you to discover or other people to comment on.



 

Just to add onto pipk's examples - the one which works for me is an inner class :

class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == myButton) {
//do some stuff
}
if (e.getSource() == anotherButton) {
//do some other stuff
}
}
}

Then when writing your code, you would create a new instance of the class within your method like :

ButtonHandler handler = new ButtonHandler();

And then when creating the button add :

myButton.addActionListener(handler);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top