In my dialog box i have a button.In it’s action listener method what does this refer to ? Is it the reference of the button or the reference of the JDialog ? What i have noticed is that it is the reference of JDialog because when in my IDE i press this. all other components and the methods of containers like dispose come up. If it is so,how it is the reference of JDialog ? It should be of JButton.
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
boolean rB_1 = jRadioButton1.isSelected();
boolean rB_2 = jRadioButton2.isSelected();
boolean rB_4 = jRadioButton4.isSelected();
if(rB_2)
new class_design().changeStatusOfMessageDisplayMode(true);
this.dispose(); // <-------- this one
}
The above code is called when a button named done is clicked from jdialog
It’s a reference to the object of the class that implements the
ActionListenerinterface you toaddActionListener.If you implemented a “standalone” class for that, it’ll be an instance of that class. If you passed in an anonymous class, then it’s a reference to an instance of that anonymous class.
If the action listener is the JDialog itself, then
thisrefers to the JDialog object.In your case, you’re using an anonymous class. So inside
actionPerformed,thisrefers to an instance of that class. But, notice that you’re calling a function that is not defined in that anonymous class. That’s where the “magic” happens: inner classes have a reference to the enclosing class.is equivalent to:
So inside
jButton5ActionPerformed,thisrefers to an object of the enclosing class, the one that created that anonymous class instance.This is explained in the JLS Inner classes and Enclosing instances: