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