The use of listeners.

Discussion in 'Java' started by Arun, Dec 28, 2004.

  1. Arun

    Arun Guest

    Hi.

    I am developing a GUI.
    I'm not quite sure what to do when i encounter listeners. To keep
    things tidy, i create a new class which implements a listener that i
    want to use, then join that listener to a component.

    For example, a listener that is called when a JTree node is selected is
    called BuildTreeSelectionListener (implementing treeselectionlistener).

    Then i use buildTree.addSelectionListener(new
    BuildTreeSelectionListener).

    What i dont get is this...

    BuildTreeSelectionListener is called, and gets the node that has just
    been selected. I now want this class to reference a method
    onNodeSelection in my gui class (SwingMainView).

    To do this, i have to set that method to static, then call
    SwingMainView.onNodeSelection(). To me this doesnt seem like good code?

    What do people usually do? Do they just implement the listener in the
    class that it has been called from, instead of creating a new class for
    it?

    On a totally different point, has anyone got a good resource for the
    proper use of keyword 'super'. I cannot find one.
    Arun, Dec 28, 2004
    #1
    1. Advertising

  2. Arun

    Chris Smith Guest

    Arun <> wrote:
    > For example, a listener that is called when a JTree node is selected is
    > called BuildTreeSelectionListener (implementing treeselectionlistener).
    >
    > Then i use buildTree.addSelectionListener(new
    > BuildTreeSelectionListener).
    >
    > What i dont get is this...
    >
    > BuildTreeSelectionListener is called, and gets the node that has just
    > been selected. I now want this class to reference a method
    > onNodeSelection in my gui class (SwingMainView).
    >
    > To do this, i have to set that method to static, then call
    > SwingMainView.onNodeSelection(). To me this doesnt seem like good code?


    You're right; that's not good code. You should give the listener a
    reference to the actual object it needs to interact with, like this:

    public class BuildTreeSelectionListener
    implements TreeSelectionListener
    {
    private SwingMainView mainView;

    public BuildTreeSelectionListener(SwingMainView view)
    {
    this.mainView = view;
    }

    public void valueChanged(TreeSelectionEvent e)
    {
    mainView.doSomething();
    }
    }

    Also, it's worth mentioning that inner classes have an implicit
    reference to their corresponding outer class, so if you're using inner
    classes (whether named or anonymous) this is no longer necessary. For
    example from within SwingMainView, you could write:

    buildTree.addSelectionListener(new TreeSelectionListener() {
    public void valueChanged(TreeSelectionEvent e)
    {
    /*
    * The following is an implicit call to the outer class,
    * using the implicit outer class reference.
    */
    doSomething();
    }
    });

    As a standard disclaimer, inner classes introduce circular dependencies
    and can interfere with reuse; generally they should only be used when
    there won't be any significant logic in the listener class itself.

    > On a totally different point, has anyone got a good resource for the
    > proper use of keyword 'super'. I cannot find one.


    Use of super isn't sophisticated enough to warrant its own resources.
    What do you want to know about it? Also, which of that keyword's two
    uses do you want to know about: calling methods, or constructor
    chaining?

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Dec 28, 2004
    #2
    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. Jon Sequeira

    session-specific Debug listeners

    Jon Sequeira, Jul 8, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    343
    Jon Sequeira
    Jul 8, 2003
  2. Bruce W...1

    Broadcast a change to all listeners?

    Bruce W...1, Nov 13, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    273
    bruce barker
    Nov 13, 2003
  3. Timo Nentwig
    Replies:
    1
    Views:
    486
    xarax
    Oct 27, 2003
  4. Chris Berg
    Replies:
    8
    Views:
    2,140
    Roland
    Aug 21, 2005
  5. Matt Kruse
    Replies:
    2
    Views:
    167
Loading...

Share This Page