JTree and JList: data reference

I want to put something together like the Windows Explorer.
A JTree on the left showing a hierarchy, a JList presenting
the contents of a selected node.

How do I get references to actual data into a JTree and
a JList? It is quite easy to fill both with strings.
But how do I get a reference to the underlying data of
each node? Is it possible to store a reference together
with the string in a node?

I want double click a node and then a window with
the respective data comes up! Consequently I will
have to supply the MouseListener with some data!

Regards, Lewis.
LewisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mnelson051997Commented:
You can store File (or TypedFile) objects within the JTree.
Then when double clicked, you can go to the File/TypedFile
and actually read it.  You can make it display properly by
creating your own TreeCellRenderer which extends JLabel.  In
the getTreeCellRendererComponent(...) method, you can get
the File/TypedFile object from the value parameter using the
getUserObject() method.  Then set the JLabel text and icon
values based on it.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LewisAuthor Commented:
Thanks for the information,
but the data I want to display is not stored
in files, I get it from another server.

So, I need to store general information in a
node, not File objects. Is it possible to
put objects of type Object there?

Do you know anything about JList, concerning
the same problem?
0
mnelson051997Commented:
You can put any object you want into a DefaultMutableTreeNode.
You can also put any object you want into a JList's
ListModel.  I believe that by default the TreeCellRenderer
and ListCellRenderer will call the object's toString()
method and get the string to display that way.  So, you can
either make your objects put out their text that way and
use the default renderer, or you can create custom cell
renderers if you want more control.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

LewisAuthor Commented:
I just yesterday found that information about
models (MVS).
Can you explain (in easy words) this model
idea to me? I haven't quite understood it!
0
mnelson051997Commented:
I'll give it a shot.  Most JFC components are made up of three
parts:

1)  The component class like JTree or JButton.  This class
stores information about the external state of itself --
things like its border, button icon, button text, background
color, etc.
2)  The model class like DefaultButtonModel or DefaultTreeModel.
This class stores data associated with the component (like the
tree nodes) and internal state information (like whether a
button is pressed, whether the component is disabled, etc.).
For JTree, the model class also allows access to the tree's
data.
3)  A look-and-feel class like BasicButtonUI.  This class
is responsible for drawing the component.  For a JButton, it
asks the JButton class for its text string and icon (if any),
and asks the DefaultButtonModel (which it gets from the JButton
class) for information about whether the button is pressed,
etc. so it can draw the component properly.  This class doesn't
store data itself because a different look-and-feel could be
plugged-in at any moment and BasicButtonUI could be replaced
with something like RoseButtonUI.  The look-and-feel class
is also responsible for reacting to events like mouse clicks
within itself.  So, when you press on a JButton, the look-and-
feel class is notified.  It then asks the model class if it
is enabled.  If so, it tells the model it is now pressed and
repaints itself.  When the mouse is then released, the look-
and-feel class tells the model class that it is no longer
pressed and generates an ActionEvent which is broadcast to
any registered ActionListeners.

This is a somewhat simplified explanation of a topic that
still confuses me, but I hope it helps.


0
LewisAuthor Commented:
So, if I understand you correctly, I have to build a
model of my tree to store the data?

Could you give me example code of
1) how to associate data with a tree node
   (in my case I would have to store something
    like: "CN=Peter,OU=Sales Dept.,O=Company Inc., C=US"
    which is the reference to an entry in an LDAP
    server.)
   
2) how to put a Listener into the whole thing
   (When I double click the tree node "Peter"
    from the example above I need to get that
    reference back and put it in a method call
    like:
        getAttributes("the_string_from_above");
    This method will then return the values
    which refer to "Peter", like his e-mail
    address, his age, ...)
0
mnelson051997Commented:
To create a DefaultMutableTreeNode with an object in it:

// Create the object.
String nodeString1 = new String( "CN=..." );
// Create the node.
DefaultMutableTreeNode node =
                    new DefaultMutableTreeNode( nodeString1 );

You create a root node and you can either explicitly use
it to create a DefaultTreeModel, or you can create a JTree
with the node and that implicitly creates a DefaultTreeModel:

JTree tree = new JTree( node );

To find out which node was double-clicked, listen for the
JTree's mouse events, and when you get a double-click, use
the MouseEvent's x and y coordinates to ask the JTree for
the node at that location.  Use the getPathForLocation(..)
method.  That gives you a TreePath, and you can use its
getLastPathComponent() method to get the actual double-
clicked node.  Then call the node's getUserObject() method
to get its contents.
0
LewisAuthor Commented:
Sorry, but it took me a while to figure this out.
You certainly wait for your points.

Final questions:
1. I can put any Object into a DefaultMutableTreeNode
   and the toString method of that object is responsible
   for delivering a String that is displayed by the tree?
2. I have to check the x and y position in order to
   find a selected node? No better way?
3. I don't have to register a Listener with every node?
   Instead I provide on [?]TreeListener and pass the node
   to it that I just found out via the TreePath?
4. What exatly are TreePaths? Objects that contain an
   absolute path?
5. What's the difference between calling JTree with
   a DefaultTreeModel versus DefaultMutableTreeNode?

Regards.
0
mnelson051997Commented:
Yes, I believe DefaultMutableTreeNode displays the user
object's toString() result.

To track selections in a JTree, register your class as a
TreeSelectionListener.  Then, when you are notified of a
tree selection change (the valueChanged() method is called),
you can get the path of the selected node using the
TreeSelectionEvent's getPath() method, then you can use1
TreePath's getLastPathComponent() method.

You definitely do not need to register with each node.  As
long as you register as a TreeSelectionListener, you will
be notified when any node is selected or deselected.

A TreePath is basically a thing that contains an array of
TreeNodes that gets you from one node (usually root) to
another node.  When you get a selected TreePath, the
node that is actually selected is the last node in the
TreePath.

When you create a JTree with a DefaultMutableTreeNode, the
JTree takes care of creating the DefaultTreeModel.  So,
there really isn't much difference.
0
LewisAuthor Commented:
Do you anything about memory usage?
Do I have to free the whole tree
recursively if I do not need it any
longer? Or is it sufficient to set
root to null and let the garbage
collector take care of it?

Anyway, here are your points.
Thanks, your advice was very helpful!
0
mnelson051997Commented:
Yes, just set root to null and the garbage collector will
deal with the rest.

Good luck!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.