Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31270 > unrolled thread

JTree add / remove nodes

Started byPhilipp Kraus <philipp.kraus@flashpixx.de>
First post2015-03-05 08:12 +0100
Last post2015-03-05 18:28 -0500
Articles 6 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  JTree add / remove nodes Philipp Kraus <philipp.kraus@flashpixx.de> - 2015-03-05 08:12 +0100
    Re: JTree add / remove nodes Jeff Higgins <jeff@invalid.invalid> - 2015-03-05 09:17 -0500
      Re: JTree add / remove nodes Philipp Kraus <philipp.kraus@flashpixx.de> - 2015-03-05 19:23 +0100
        Re: JTree add / remove nodes Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-03-05 14:18 -0500
          Re: JTree add / remove nodes Philipp Kraus <philipp.kraus@flashpixx.de> - 2015-03-05 23:59 +0100
            Re: JTree add / remove nodes Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-03-05 18:28 -0500

#31270 — JTree add / remove nodes

FromPhilipp Kraus <philipp.kraus@flashpixx.de>
Date2015-03-05 08:12 +0100
SubjectJTree add / remove nodes
Message-ID<md8vkk$2905$1@ariadne.rz.tu-clausthal.de>

[Multipart message — attachments visible in raw view] — view raw

Hello,

I have got a tree dataset and I would like to visualize the data with a 
JTree component. The user should only brows in the tree, so no 
modifcation on the nodes is needed.
But I don't understand, which I can add and remove nodes from the tree. 
I have created this data

protected DefaultMutableTreeNode m_root = new DefaultMutableTreeNode( 
"myRoot" );
protected final JTree m_tree = new JTree( m_root );
protected final TreeModel m_model = new DefaultTreeModel( m_root );

and an empty tree is shown. But how can I add at this new nodes? My 
data are path strings like "root / subnode / subsubnode / leaf", so can 
I addthe full path
to my tree which node / leaf creation on the fly? So I would push the 
the string to the tree and all parts should be created if it not 
exists. At the same way I get a
path which should remove from the tree.

I'm a little bit overwhelmed with the JTree.

Thanks a lot

Phil

[toc] | [next] | [standalone]


#31272

FromJeff Higgins <jeff@invalid.invalid>
Date2015-03-05 09:17 -0500
Message-ID<md9ogk$6lv$1@dont-email.me>
In reply to#31270
On 03/05/2015 02:12 AM, Philipp Kraus wrote:
> Hello,
>
>
> I have got a tree dataset and I would like to visualize the data with a
> JTree component. The user should only brows in the tree, so no
> modifcation on the nodes is needed.

JTree.DynamicUtilTreeNode has a static
createChildren(DefaultMutableTreeNode parent, Object children)
method which is convenient for this.

>
> But I don't understand, which I can add and remove nodes from the tree.
> I have created this data
>
>
> *protected *DefaultMutableTreeNode *m_root *= *new
> *DefaultMutableTreeNode( *"myRoot" *);
> *protected final *JTree *m_tree *= *new *JTree( *m_root *);
> *protected final *TreeModel *m_model *= *new *DefaultTreeModel( *m_root *);
>
> and an empty tree is shown.
 >
 > But how can I add at this new nodes?
> My data are path strings like "root / subnode / subsubnode / leaf", so can I
> addthe full path to my tree which node / leaf creation on the fly?

> So I would push the the string to the tree and all parts should be created if
> it not exists. At the same way I get a path which should remove from the tree.

Now your tree has become dynamic?
You are responsible for implementing these behaviors
upon the TreeModel and MutableTreeNode interfaces.

>
> I'm a little bit overwhelmed with the JTree.
>

[toc] | [prev] | [next] | [standalone]


#31273

FromPhilipp Kraus <philipp.kraus@flashpixx.de>
Date2015-03-05 19:23 +0100
Message-ID<mda6v0$2hdn$1@ariadne.rz.tu-clausthal.de>
In reply to#31272
On 2015-03-05 14:17:28 +0000, Jeff Higgins said:

> On 03/05/2015 02:12 AM, Philipp Kraus wrote:
>> I have got a tree dataset and I would like to visualize the data with a
>> JTree component. The user should only brows in the tree, so no
>> modifcation on the nodes is needed.
> 
> JTree.DynamicUtilTreeNode has a static
> createChildren(DefaultMutableTreeNode parent, Object children)
> method which is convenient for this.

Greate thanks, this work's.

My question is, the createChildren adds only nodes until the parent,
but how can I add a full path similar to the linux command "mkdir -p"
which creates all directories of the path, if it not exists.

Thanks

Phil

[toc] | [prev] | [next] | [standalone]


#31274

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2015-03-05 14:18 -0500
Message-ID<mdaa4l$ji1$1@dont-email.me>
In reply to#31273
On 3/5/2015 1:23 PM, Philipp Kraus wrote:
> On 2015-03-05 14:17:28 +0000, Jeff Higgins said:
>
>> On 03/05/2015 02:12 AM, Philipp Kraus wrote:
>>> I have got a tree dataset and I would like to visualize the data with a
>>> JTree component. The user should only brows in the tree, so no
>>> modifcation on the nodes is needed.
>>
>> JTree.DynamicUtilTreeNode has a static
>> createChildren(DefaultMutableTreeNode parent, Object children)
>> method which is convenient for this.
>
> Greate thanks, this work's.
>
> My question is, the createChildren adds only nodes until the parent,
> but how can I add a full path similar to the linux command "mkdir -p"
> which creates all directories of the path, if it not exists.

     Divide the components of your path into parent, child, grandchild,
and so on.  If the parent is not already present in the tree's top
level, add it.  Then if the child is not already present in the
parent's level, add it.  Then if the grandchild is not already present
in the child's level, add it ...

     See

https://docs.oracle.com/javase/tutorial/uiswing/components/tree.html


-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

[toc] | [prev] | [next] | [standalone]


#31275

FromPhilipp Kraus <philipp.kraus@flashpixx.de>
Date2015-03-05 23:59 +0100
Message-ID<mdan49$2kq4$1@ariadne.rz.tu-clausthal.de>
In reply to#31274
On 2015-03-05 19:18:21 +0000, Eric Sosman said:

> On 3/5/2015 1:23 PM, Philipp Kraus wrote:
>> On 2015-03-05 14:17:28 +0000, Jeff Higgins said:
>> 
>>> On 03/05/2015 02:12 AM, Philipp Kraus wrote:
>>>> I have got a tree dataset and I would like to visualize the data with a
>>>> JTree component. The user should only brows in the tree, so no
>>>> modifcation on the nodes is needed.
>>> 
>>> JTree.DynamicUtilTreeNode has a static
>>> createChildren(DefaultMutableTreeNode parent, Object children)
>>> method which is convenient for this.
>> 
>> Greate thanks, this work's.
>> 
>> My question is, the createChildren adds only nodes until the parent,
>> but how can I add a full path similar to the linux command "mkdir -p"
>> which creates all directories of the path, if it not exists.
> 
>      Divide the components of your path into parent, child, grandchild,
> and so on.  If the parent is not already present in the tree's top
> level, add it.  Then if the child is not already present in the
> parent's level, add it.  Then if the grandchild is not already present
> in the child's level, add it ...

Okay I must do my traversing myself, so I have hoped for a build-in function

Phil

[toc] | [prev] | [next] | [standalone]


#31276

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2015-03-05 18:28 -0500
Message-ID<mdaope$jbq$1@dont-email.me>
In reply to#31275
On 3/5/2015 5:59 PM, Philipp Kraus wrote:
> On 2015-03-05 19:18:21 +0000, Eric Sosman said:
>
>> On 3/5/2015 1:23 PM, Philipp Kraus wrote:
>>> On 2015-03-05 14:17:28 +0000, Jeff Higgins said:
>>>
>>>> On 03/05/2015 02:12 AM, Philipp Kraus wrote:
>>>>> I have got a tree dataset and I would like to visualize the data
>>>>> with a
>>>>> JTree component. The user should only brows in the tree, so no
>>>>> modifcation on the nodes is needed.
>>>>
>>>> JTree.DynamicUtilTreeNode has a static
>>>> createChildren(DefaultMutableTreeNode parent, Object children)
>>>> method which is convenient for this.
>>>
>>> Greate thanks, this work's.
>>>
>>> My question is, the createChildren adds only nodes until the parent,
>>> but how can I add a full path similar to the linux command "mkdir -p"
>>> which creates all directories of the path, if it not exists.
>>
>>      Divide the components of your path into parent, child, grandchild,
>> and so on.  If the parent is not already present in the tree's top
>> level, add it.  Then if the child is not already present in the
>> parent's level, add it.  Then if the grandchild is not already present
>> in the child's level, add it ...
>
> Okay I must do my traversing myself, so I have hoped for a build-in
> function

     I don't know of one.  Certainly the "divide into components" part
is highly data-specific, and thus unlikely to be offered as a utility
method.  Even if you fed the utility a List of component parts, it's
not entirely clear what should happen: For example, if you start with
the tree

	a/
	   b/
	   c/

and add a/b/d, should the JTree display

	a/
	   b/
	      d
	   c/

or

	a/
	   b/
	      d
	   b/
	   c/

or

	a/
	   b/
	   c/
	a/
	   b/
	   d


or ...?  Policy decisions of this sort are more the province of the
TreeModel than of the JTree, I think.

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web