Solved

JTree-Wanted in a Expanded Mode!!!!

Posted on 2001-08-01
7
640 Views
Last Modified: 2012-08-13
Hai
   I am using a JTree , at times nodes can have child nodes also. The problems now is when ever a new node added or removed, which do happen very frequently,either its a child node or node, I am creating/refreshing the tree. This is keeping my tree in collaped mode. I want my tree to be always expanded mode. Please help me for this problem.
Thanks in advance.
Rajesh.Sp Kann                                
0
Comment
Question by:Rajeshsp
7 Comments
 
LVL 15

Accepted Solution

by:
ozymandias earned 25 total points
ID: 6340734
Don't create or refresh the whole tree.

You can just call the fire event methods of your TreeModel.

If you have a TreeNode called tn a TreeModel called model and you add a child you can do :

 
  tn.add(newnodegoeshere);
  model.nodesWereInserted(tn, new int[]{indexOfAddedChild});

Similarly if you delete a child you can call model.nodesWereRemoved(tn, new int[]{indexOfDeletedChild}, new Object[]{deletedChildNode3}).




 
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6340763
If you want an example, download this :

  http://ozymandias.webjump.com/dndjtree2.java

To add a tree node drag an icon off the toolbar and drop it on the node that will be the new node's parent, or right-click on a node and select add from the popup menu.

You can also rename and delete nodes.

On each of these changes the onlt refresh that takes place is the refresh of the parent node, rather than the whole tree.
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6340772
BTW, you can also call the reload method of your TreeModel and specify which node you want reloaded :

  model.reload(tn);
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:Rajeshsp
ID: 6343426
Hello
     That's ok ,in my application when i add a leaf node the Parnt Node(which allows a maximum of 5child nodes) Text(Java) should show the Count(0/05) as:
 Java[1/05] as Title(Parent) and when the second node added the Title of Parent changes toJava[02/05]

Then i take the count from Database so i have to refresh
instantly.Any solution

Regards
Rajesh.Sp Kann
0
 
LVL 16

Expert Comment

by:Valeri
ID: 6343758
read "DefaultTreeModel" help. Take a look at methods :
insertNodeInto, removeNodeFromParent and nodeChanged....

Don't call the fire event methods of your TreeModel...
Just update the model using insertNodeInto and removeNodeFromParent methods.

Best regards
Valeri
0
 
LVL 16

Assisted Solution

by:Valeri
Valeri earned 25 total points
ID: 6343769
>
That's ok ,in my application when i add a leaf node the Parnt Node(which allows a maximum of 5child
nodes) Text(Java) should show the Count(0/05) as:
Java[1/05] as Title(Parent) and when the second node added the Title of Parent changes toJava[02/05]

Then i take the count from Database so i have to refresh
instantly.Any solution
<
Take a look at DefaultMutableTreeNode class help. setUserObject method. You must overwrite toString() method.

to refresh instantly invoke nodeChanged method of DafaultTreeModel
0
 
LVL 5

Expert Comment

by:lwinkenb
ID: 8895255
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Split points between Valeri and ozymandias
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
lwinkenb
EE Cleanup Volunteer
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
stackato and cloud 4 85
type mismatch (Object[] to double[] 4 24
eclipse buid path vs tomcat lib path 10 22
Unhandled exception type Exception 18 28
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question