• Post Reply Bookmark Topic Watch Topic
  • New Topic

add a swing component to a tree  RSS feed

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can I create a tree structure consisting of checkboxes?I mean can I replace nodes with swing components.

Any suggestions are welcome.
Thanks
Pomchi
[This message has been edited by Pomchi Bedi (edited April 03, 2001).]
[This message has been edited by Pomchi Bedi (edited April 03, 2001).]
 
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you mean can you add swing components to the nodes, the answer is "yes". I had to do this for a project once. As I understand it, you need to create a "template" (I hate using this word, but there it is!) for the objects you're going to be putting into the nodes. This template tells Swing how to interpret what you add to the nodes. This is done by creating a custom renderer and custom editor for the JTree class.
In my case, I needed to put an EditorWindow (a class that extended JPanel) into the nodes. The EditorWindow could (and did)contain any Swing components. I defined the renderer/editor like this:

To use them, I did this (simplified):

Make sense? I stumbled around in the dark to create this, but it worked. Confusing? Let me know if you need anything clarified. I'm not sure I understand this 100% myself ( ), but I'll do my best.
 
Pomchi Bedi
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Matt,
Thanks for the prompt reply.
I know my question is a bit confusing.But still....I don't want to add components to nodes.I want to place my components instead of nodes.
In my project I have a few groups/packages and each group contains a few features.I want a tree like structure in which groups would be parents and features would be leaves....and when I select a parent node(group) all the corresponding leaves should get selected.In this way I will be able to select features gropwise just by a few clicks and I will also be able to deselect the featues that I do not want.
Just one question about your code ,I could not understand what is DisplayTree?Can you explain.
Any other suggestion will also help.
Thanx again
Pomchi
 
Matt Senecal
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay,
I'm not sure I get what you mean when you say that you "don't want to add components to nodes" and that you want to "place your components instead of nodes".
A node is just a point on a tree that holds objects of some sort. It's not an issue of "node = bad, my stuff = good", but an issue of "node = good if I can put my own data into it". My understanding is that you can put anything you want in a node as long as you define an editor and renderer for it. I'm not sure you can completely eliminate and replace a node. That would destroy the structure of the JTree.
In your case I don't think what you're trying to do is too difficult. Here's how I would approach it (and there may be a better way):
Define an editor/renderer for your tree to contain a JCheckBox (or maybe a JPanel). The ones I gave you will work with some very minor modifications.
You say you want a parent class that contains features as leaves. This parent class is a node with features as children. It contains references to all the features attached to it so that if it gets selected, all the features attached to it are selected as well:

This is your feature class. It just extends JCheckBox.

Then create a JTree, setting it's editor and renderer to your cusom ones. For each node create a DefaultMutableTreeNode, setting it's data to one of your custom JCheckBoxes:

If you used a renderer that returned a JPanel, you'd have to do something like this:

Your JTree now contains JCheckBoxes at the nodes. The features can be selected indivisually, and if a parent is selected, all the features associated with that parent are selected as well.
This is just an outline. You'll have to add code to refresh the tree and do some other things, but hopefully this will give you an idea of something you can do.
In my code, DisplayTree was a custom class I created that extended JTree. It didn't do anything fancy.
I suggest you take a look at:
http://web2.java.sun.com/docs/books/tutorial/uiswing/components/tree.html
for more info.
Originally posted by Pomchi Bedi:
Hi Matt,
Thanks for the prompt reply.
I know my question is a bit confusing.But still....I don't want to add components to nodes.I want to place my components instead of nodes.
In my project I have a few groups/packages and each group contains a few features.I want a tree like structure in which groups would be parents and features would be leaves....and when I select a parent node(group) all the corresponding leaves should get selected.In this way I will be able to select features gropwise just by a few clicks and I will also be able to deselect the featues that I do not want.
Just one question about your code ,I could not understand what is DisplayTree?Can you explain.
Any other suggestion will also help.
Thanx again
Pomchi

 
Pomchi Bedi
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Matt!
I got what I wanted from your code.Just one problem.......How do I add the child node (Feature) to Parent.

Parent theParent = new Parent("Feature Set #1");
DefaultMutableTreeNode top = new DefaultMutableTreeNode(theParent, true);

Feature theFeature = new Feature("First Feature");
DefaultMutableTreeNode n1=new DefaultMutableTreeNode(theFeature,false);
theParent.addChild(???);

JTree tree=new JTree(top);


Regards
Pomchi
 
Matt Senecal
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm... I banged out the code I gave you in a hurry, and I see that I didn't make associating a feature with a parent an easy thing! Hmm...
OK, first of all, when you add a feature to a parent, you need to add the feature node to the parent node. You're already getting the idea. To add to your code:

It may be that once you have added a Parent to a node, you can still alter it's internal data structures. That is:

may work, but to be honest, I've never tried it. This is the way I'd try first.
Another rather cumbersome way that works with the code I gave you is to create the Feature objects ahead of time and add them to the Parent objects before everything is put in the nodes and tree. This will create the proper associations:

Hope this helps!

Originally posted by Pomchi Bedi:

Thanks Matt!
I got what I wanted from your code.Just one problem.......How do I add the child node (Feature) to Parent.

Parent theParent = new Parent("Feature Set #1");
DefaultMutableTreeNode top = new DefaultMutableTreeNode(theParent, true);

Feature theFeature = new Feature("First Feature");
DefaultMutableTreeNode n1=new DefaultMutableTreeNode(theFeature,false);
theParent.addChild(???);

JTree tree=new JTree(top);


Regards
Pomchi

 
Pomchi Bedi
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Matt,
Check this out!


This is what I wanted to do.Thanks to you....without your help I could not have done this.

Thanks again
Pomchi

[This message has been edited by Pomchi Bedi (edited April 06, 2001).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!