Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

To set the data from JTree to JTextArea

 
Meeraj
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

I use a JTree. Whatever data the JTree consists, that must be put into a JTextArea.


Thanks in advance.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Pinku Tinku",

Please take a look at the ranch naming policy, which states, among other things, that obviously fictitious names are not allowed.
You can adjust your screen name to comply to the policy by clicking here.

Coming back to your question.
The data displayed in the tree is encapsulated in the model.
Check out the TreeModel API where you can find suitable methods to traverse the tree nodes and pick out the information you want to display.
 
Meeraj
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your idea.
Definitely i will change my display name.
What i want to do is The entire data of JTree has to be diplayed in JTextArea. How do i get this.

Thanks
 
Meeraj
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I can write it to a file by



it is throwing an exception java.io.NotSerializableException:
Any ideas.


Thanks
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Pinku Tinku",
I suggest you take the naming policy seriously. Name violators stand the chance of getting their account suspended. You can change your display name here.
Before you post again, you are expected to adjust your screen name.
 
Meeraj
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have changed my display name.
Can you please suggest.

My requirement is the content of the JTree must be set to the text file.

Thanks
 
Campbell Ritchie
Marshal
Posts: 52621
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Meeraj:
I have changed my display name.


But it doesn't match the policy; the next you know about this might be "account closed."
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> I use a JTree. Whatever data the JTree consists, that must be put into a JTextArea.

OK, that's a requirement, what's the problem?
 
Robert Noack
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm also looking for an answer to this question...
what is the easiest way to get all the data from a JTree to any kind of text format.

I want to save it as XML or TXT, but that is not important... There just doesn't seem to be a great way to get that info out of there quickly.

Also, instead of complaining of the naming policy can't you say something like "I don't know the answer to your question" as well if you don't actually know the answer?
Or say the naming issue, and then answer the question.

Not answering it at all just seems annoying to me, but I guess I'm too polite ?
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JTreeModel is a tree which consists of TreeNode objects. You would have to write code which recursively went through those objects and generated your required output. If you used DefaultMutableTreeNode objects, I'm pretty sure that there's a method which returns an Enumeration which produces each of the nodes in the right order. You could do whatever you needed to process those nodes and generate the required output.

As for your complaint about the naming policy, we have a forum (Ranch Office) for that kind of discussion. I suspect a lot of people don't really think somebody deserves an answer until they comply with the policy, but that isn't a hard and fast rule by any means. Don't hesitate to bring it up in the Ranch Office, though, if you think it's a bad thing when that happens.
 
Brian Cole
Author
Ranch Hand
Posts: 924
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Meeraj / Pinku Tinku / ? wrote:

I use a JTree. Whatever data the JTree consists, that must be put into a JTextArea.



That's not really a question, but there are many conceivable ways to convert a TreeModel into String representation.

Here's one:
So you could call something like yourTextArea.setText( toString(yourTree.getModel()) ).

(You would probably also want to set a fixed-width font.)


>
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!