Reputation: 37
I have a class called Game and a button in it with the following code
public Game(){
(some code..)
btn_start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
(some code..)
timer_task = new MyTask();
(some code..)
}
});
}
}
My problem is that MyTask requires an object of type "Game" (for various reason). How can i send to MyTask() the class that my actionListener method is in? Is there such a thing in Java?
I tried using .this
but it says that it refers to the ActionListener
.
Upvotes: 1
Views: 48
Reputation: 53531
You might want to consider having a private method in your class Game
to create MyTask
. Something like
public void someMethod()
{
...
btn_start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
handleStart();
}
});
...
}
private void handleStart()
{
(some code..)
timer_task = new MyTask(this);
(some code..)
}
Upvotes: 1
Reputation: 347184
Try using Game.this
instead
This basically tells Java to use the outter class reference of this
instead
Upvotes: 4