Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > The use of listeners.

Reply
Thread Tools

The use of listeners.

 
 
Arun
Guest
Posts: n/a
 
      12-28-2004
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.

 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      12-28-2004
Arun <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Could not use ''; file already in use. M K ASP .Net 11 04-09-2008 11:35 AM
where to use CPLD & where to use FPGA? kulkarku@math.net VHDL 6 03-06-2006 07:27 AM
How do I know when to use the Viewstate and when to use the posted data? :-) Simon ASP .Net 1 11-09-2004 02:32 AM
Can I use XPath or something to a remote Mac or Linux box and just query an xml file, not using web services and use encyrption? jake ASP .Net 0 07-06-2004 02:16 PM
Cannot use the profile "default" because it is in use, not. please.post@yur.re.ply Firefox 1 07-04-2004 03:41 AM



Advertisments