-1

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

program-o-steve
  • 2,630
  • 15
  • 48
  • 67

4 Answers4

3

This looks like code from Netbeans. jButton5ActionPerformed is standing on it's own as a method inside your JDialog. This refers obviously to the JDialog class that includes the method.

    jButton1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            this <---Refers to the ActionListener.
        }
    });
    this <---Refers to the class (JDialog in your case)

In order to avoid getting into a situation like this, Netbeans create an new method jButton1ActionPerformed outside the anonymous ActionListener, and call it.

    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    this <---obviously refers to the JDialog
}
Costis Aivalis
  • 13,680
  • 3
  • 46
  • 47
  • `this <---obviously refers to the JDialog` Can you please explain it – program-o-steve Dec 11 '11 at 10:59
  • In my second code sample, you have a call to a jButton1ActionPerformed method. When you used "this", inside this method it refers to the instance of the enclosing class, which in your case is a JDialog, since it is a method of this class. In the first code sample the first "this" refers to the ActionListener object since it is a method of the ActionListener class and the second to the JDialog. – Costis Aivalis Dec 11 '11 at 17:37
3

It's a reference to the object of the class that implements the ActionListener interface you to addActionListener.

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 this refers to the JDialog object.

In your case, you're using an anonymous class. So inside actionPerformed, this refers 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.

jButton5ActionPerformed(evt);

is equivalent to:

myenclosingobject.jButton5ActionPerformed(evt);

So inside jButton5ActionPerformed, this refers 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:

An instance i of a direct inner class C of a class O is associated with an instance of O, known as the immediately enclosing instance of i. The immediately enclosing instance of an object, if any, is determined when the object is created (§15.9.2).

Mat
  • 202,337
  • 40
  • 393
  • 406
  • `If you passed in an anonymous class, then it's a reference to an instance of that anonymous class.` That anonymous class never extended the JDialog class. then how i am able to access methods such as `dispose` – program-o-steve Dec 11 '11 at 10:52
  • 1
    I never said it did. And I don't think you're passing an anonymous class. The `this` inside your `jButton5ActionPerformed` code will (has to) be an instance of the class that defines that method. It can't be an instance of anything else, and I don't think that method is in an anonymous class. – Mat Dec 11 '11 at 10:55
  • please see how the method has been called. _EDITED_ – program-o-steve Dec 11 '11 at 10:58
  • Edited now that you clarified your use case. – Mat Dec 11 '11 at 11:07
1

1) you have to read How to Make Dialogs

2) if you create JDialog only once per Applications life_cycle then call JDialog#dispose(), otherwise create JDialog only once time and reuse that for another actions/events

mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

In your case, I suppose this the scenario,

Probably in netbeans, you created a jframe, then added a jdialog.Then added a jbutton, now double clicked the button,to write the code for action performed (or right click,events>action>actionperformed)

Note : then added a jdialog , I meant you dragged n dropped it to the frame.

In the given scenario, this refers to the jframe and not the jdialog

COD3BOY
  • 11,964
  • 1
  • 38
  • 56