You need a component to call repaint()
on. The simplest solution is to call repaint()
like this:
((JComponent)e.getSource()).getTopLevelAncestor().repaint();
The issue is that your ActionListener
is declared as a static member class, so it does not have access to the non-static members of the enclosing class because it is not associated with an instance of the enclosing class. Usually I put all of my GUI initialization code inside the constructor for my own JPanel
subclasses. I also use anonymous inner classes for listeners. You can just as easily use named classes as long as they are not static. Then you can call JPanel methods inside the the listeners methods.