So I have A MouseListener class inside a public class with a few methods.
I have attached the mouseListener to a component in the public class.
The problem is I can’t figure out a simple way to call the methods in the public class, whenever I say for example this.showRemove(); the scope is from within the handler class and not the public class. Here is some example code
public class Game {
public Game() {
JPanel pnl = new JPanel();
pnl.addMouseListener(new GameMouseListener());
}
public void showRemove(){
//Code Here
}
class GameMouseListener implements MouseListener {
public void mouseClicked(MouseEvent e) {
this.showRemove(); //Can't Find Symbol Here
}
}
}
When you use
thisin an inner class, you are referring to the instance of the inner class, not the host class.Since your inner class is not a static inner class, you can access the reference to the host class by using it’s class name like so:
Unless the method you’re calling is also in the inner class, you can avoid the explicit naming and can simply use the method name: