2

I want to create a JTree, and each node of the JTree has a name, and when you click on the node, a textarea will be expanded, I did some research online but didn't find anything useful. Could anyone tell me if this possible to achieve? if yes, could you please post a simple code sample?

1 Answer 1

1

You dont need JTextAreas, JTree has the ability built-in to make Nodes editable. Take a look at this example:

http://www.roseindia.net/java/example/java/swing/JTreeEditable.shtml

I just tried it out to make sure it works. Compile & Run. Click on one of the Nodes then press F2 to edit it. Please comment if you have questions or if this is not what you are looking for.

4
  • Thank you eznme! However, I think I didn't express my problem clearly, the tree I want to construct contains some comments and notes created by the user, the name of the nodes of the tree would be the commenter's name (or a title of the comment), in the example you provided, it only allows me to edit the name of the node, I want to be able to do: when the user expands a node, a textarea gets expanded and the user would type in their comment in the text area. I'm not sure if this makes my question clearer, please don't hesitate to comment if it's still confusing to you!
    – JYH
    Mar 4, 2011 at 21:45
  • Ah, i understand. What you need is a custom TreeCellRenderer, take a look here: download.oracle.com/javase/1.4.2/docs/api/javax/swing/tree/… Mar 4, 2011 at 21:58
  • 1
    That's difficult tho, if you want something that is easier implemented you could render a text-area below your tree, then use TreeSelectionEvents (download.oracle.com/javase/1.4.2/docs/api/javax/swing/event/…) to change which node's extra-text is currently edited. Mar 4, 2011 at 22:00
  • considering the custom TreeCellRenderer, here is an example: java2s.com/Code/Java/Swing-JFC/TreeCellRenderer.htm Mar 4, 2011 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.