Reputation: 13
So I have a window with a JButton
called "start". What I would like to happen is when that button is clicked it will run a separate class I have already created. Is this possible and if so how would I go about doing it?
Thanks for all your help
Here's what I have so far
JButton start = new JButton ("Play");
frame.add(start);
start.addActionListener(//not sure what goes here, i would like to call other class here)
By the way, the other class being called is falling in case that matters
Upvotes: 1
Views: 3252
Reputation: 7097
There are many ways to do this. If you want to use another class that has already implemented ActionListener
then you can absolutely create an object of that class and then insert is as such:
//Another class that you're already created is named MyActionListener
// and implements ActionListener for this example
MyActionListener mal = new MyActionListener();
JButton start = new JButton ("Play");
frame.add(start);
start.addActionListener(mal);
However, this might not make any sense because you need to reference variables that are members of the current class's instance. In that case you want the current class to implement ActionListener
as such:
class MyClass implements ActionListener {
public MyClass() {
JButton start = new JButton ("Play");
frame.add(start);
start.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
//do whatever you need to do here
}
}
The this
keyword means that you are referring to the current instance of this class. Therefore it will use the actionPerformed
method that you implemented within this class. You can instantiate an object of your other class within the actionPerformed
method and then make any calls on that object as usual or if it's a member of this class, just call the member's function directly. Additionally, if your referring to static methods on that second class you can also calls those directly within the actionPerformed
method.
There is one other option, which is to use an anonymous inner class. This is usually used if you want to have multiple actionPerformed
methods within a single class that aren't shared between different components' event handler's registrations (ie. just use it for this single start button). Here's how to use that method:
JButton start = new JButton ("Play");
frame.add(start);
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//do whatever you want here for whenever start is clicked
}
});
As with the other options, you can also instantiate a second class in the actionPerformed
method here or call a static method from another class. However, if you were planning on calling a method that is on a variable that is a member of the outer class (MyClass
in this case), then you need to use this syntax this.MyOtherClass.method()
. The reason being is that the this
keyword in this case provides you with access to the outer class inside of the anonymous inner class.
Upvotes: 2
Reputation: 1230
You(your class) can implement ActionListener to handle when the button is clicked.
public class YourClass implements ActionListener {
public YourClass() {
JButton start = new JButton ("Play");
frame.add(start);
start.addActionListener(this);
}
public void actionPerformed(ActionEvent arg0) {
// Call other class
}
}
Upvotes: 3