Refering to parent component from anonymous class

S

Sameer

Many times in GUI programming we need to write action listeners
with the help of an anonymous class.
How to refer to the parent frame from these anonymous classes if
we need a reference to it.

For example in showing a dialog from the anonymous class, we
need to pass a reference of parent component like:

public class GUI extends Frame {
private void createMenus() {
menuItemExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
void showMessageDialog(Component parentComponent,
Object message);
}
});
}
}

We can't use 'this' in this situation because it will refer to the
anonymous class.

Is there any wayout to pass refernce of the parent component to
the anonymous class?

-Sameer
 
P

Patricia Shanahan

Sameer said:
Many times in GUI programming we need to write action listeners
with the help of an anonymous class.
How to refer to the parent frame from these anonymous classes if
we need a reference to it.

For example in showing a dialog from the anonymous class, we
need to pass a reference of parent component like:

public class GUI extends Frame {
private void createMenus() {
menuItemExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
void showMessageDialog(Component parentComponent,
Object message);
}
});
}
}

We can't use 'this' in this situation because it will refer to the
anonymous class.

Is there any wayout to pass refernce of the parent component to
the anonymous class?

-Sameer

Qualify "this" with the name of the enclosing class: GUI.this

Patricia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top