I have a problem in how to addActionListener for a button in static method
here is my static method
private static void initComponents() {
btnExit = new JButton("Exit");
btnExit.addActionListener(this); <==== this is error
}
do you guys have any idea
in how to add a listener so that the button response when clicked
Since it is a static method, there is no
this. You can use several options here: anonymous class, new instance of a class which implementsActionListener, … .Some sample code which uses the anonymous class
You might want to read this article about static methods/variables and this article about the
thiskeyword as well to obtain a better understanding on whythiswill not work in a static context