Access ActionListener in java
Hi
I have a problem in my code. I have posted it below. In the code I have two classes.
Code:
class 1:
..
..
..
private class Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{type = e.getActionCommand();}}
..
..
class 2:
..
..
..
private class AddListener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ Video video = new Video(?); }}
..
..
I don't know how to refer to the ActionListener class of class 1 from the ActionListener class of class 2. Please help me.
Re: Access ActionListener in java
Hi,
I think you have declared the video inside the method, so it will be visible inside that method only. If it is not inside class2 method you can not access it. Please check your program again. Try it you self, if it is not possible we will help you. Next time post your updated code so that we can review it.
Re: Access ActionListener in java
Hi
Thanks for the reply. The other way I was thinking to get rid of the Action Listener in class1. By make that change I have to store the button that was clicked in a method and access that method from Action Listener in the class2. This is just my thinking, I do not know if this will work. Any suggestions on this, please advice.
Re: Access ActionListener in java
Hi
I think your code should be something like this, see the posted code below
Code:
class 1:
private String type = "";
..
..
..
private class Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{type = e.getActionCommand();}}
public String getType() {
return type;
}
..
..
Code:
class 2:
private Class1 class1Reference;
public void setClass1(Class1 class1Reference) {
this.class1Reference = class1Reference;
}
..
..
..
private class AddListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
String type = class1Reference.getType();
Video video = new Video(type);
}
}
..
..
If any more queries do post back. And even if solved post, what did you do to solve it.