The use of listeners.

A

Arun

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.
 
C

Chris Smith

Arun said:
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
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,271
Latest member
BuyAtenaLabsCBD

Latest Threads

Top