Refering to parent component from anonymous class

Discussion in 'Java' started by Sameer, Aug 28, 2005.

  1. Sameer

    Sameer Guest

    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
     
    Sameer, Aug 28, 2005
    #1
    1. Advertising

  2. Sameer wrote:
    > 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
     
    Patricia Shanahan, Aug 28, 2005
    #2
    1. Advertising

  3. Sameer

    Roedy Green Guest

    Roedy Green, Aug 28, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TmljayBaZHVuaWM=?=

    Refering to web page class from another class

    =?Utf-8?B?TmljayBaZHVuaWM=?=, Feb 8, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    445
    Karl Seguin [MVP]
    Feb 8, 2006
  2. E11
    Replies:
    1
    Views:
    4,901
    Thomas Weidenfeller
    Oct 12, 2005
  3. Reporter
    Replies:
    3
    Views:
    489
    Mike Schilling
    May 12, 2007
  4. JackRazz

    Finding a Component's (ComponentModel.Component) Parent Form

    JackRazz, Mar 4, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    485
    John Saunders
    Mar 4, 2004
  5. Toby Rodwell

    Refering to an object's 'parent'

    Toby Rodwell, Oct 5, 2007, in forum: Ruby
    Replies:
    3
    Views:
    144
    ara.t.howard
    Oct 5, 2007
Loading...

Share This Page