🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #16887

Re: JTree listeners

#16887
From: Paul Furbacher
Date: Tue, 12 Jan 1999 00:00
58 lines
2724 bytes
Leo Cohen wrote:
 
> I am trying to add a JTree listener to my program. The code
> 
>       JTree viewTree = new JTree();
>       TreeModel mdl = new ViewTreeModel(st, root);
>       viewTree.setModel(mdl);
>       mdl.addTreeModelListener(?);
> 
> needs a parameter for the addTreeModelListener() call. JTree has the
> line  JTree.this.getModel().addTreeModelListener(this); in the method
> AccessibleJTree() within the AccessibleJTree class.

The code you cite from JTree is in the class AccessibleJTree, which is 
nested in JTree.  Within in the scope of AccessibleJTree, "this" refers
to an instance of AccessibleJTree.  The other "this", i.e., "JTree.this"
refers to the "this" of the "owning" class, JTree.

Perhaps writing it this way makes a bit more sense:

  JTree.this.getModel.addTreeeModelListener(AccessibleJTree.this);

You'll notice that AccessibleJTree implements "TreeModelListener",
and indeed, it implements the four methods of the TreeModelListener
interface: 

 o treeNodesChanged()
 o treeNodesInserted()
 o treeNodesRemoved()
 o treeStructureChanged()

JTree does not implement these, and therefore, it can't be 
the object referenced by the "this" in "....addTreeModelListener(this)"
from above.

> Question: since the proper form for
> addTreeModelListener(TreeModelListener tml) includes a parameter as
> shown, why does addTreeModelListener(this) work in JTree class when this
> refers to JTree (or AccessibleJTree)?

You have basically answered your question: it's the latter, AccessibleJTree;
and the code is in AccessibleJTree which is nested in JTree.

> I get a compiler error when I mimick this code with the line
> mdl.addTreeModelListener(viewTree);

For kicks, you can get access to the AccessibleJTree instance via 

  AccessibleJTree ajt = myJTree.getAccessibleContext();

however, it's doesn't do any good to use "ajt" as the parameter.

Instead, you are probably thinking to add your extended JFrame 
as a tree model listener.  If so, it will have to implement 
the TreeModelListener interface.


Paul Furbacher
Technical Details
Message-ID: <369C1CE7.C73B04FF@bilbo.bio.purdue.edu>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <369BA4EA.A1D99B54@cs.virginia.edu>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,d0d912cc934860da
X-Google-Attributes: gidf74b3,public
From: Paul Furbacher <pf@bilbo.bio.purdue.edu>
Subject: Re: JTree listeners
Date: 1999/01/12
Message-ID: <369C1CE7.C73B04FF@bilbo.bio.purdue.edu>
X-Deja-AN: 432004422
Content-Transfer-Encoding: 7bit
References: <369BA4EA.A1D99B54@cs.virginia.edu>
To: Leo Cohen <lcohen@cs.virginia.edu>
Content-Type: text/plain; charset=us-ascii
Organization: Biological Sciences, Purdue Univ.
Mime-Version: 1.0
Reply-To: pf@bilbo.bio.purdue.edu
Newsgroups: borland.public.jbuilder.java.language