简体   繁体   中英

How to display JTree file in JTextArea

In my program i have a JTree which is being populated by a number of folders and files. When you select a file in the JTree I want that file to display in the JTextArea .

I no you can use getLastSelectedPathComponent() to get the last selected item from the JTree which can be used to get the chosen file from the tree but this is then a node of the tree... is there a way to get the text from this node like using a FileReader and a BufferReader ?

I would put File objects in the tree and use a cell renderer to show (render) them as required.

See File Browser GUI for an example. The example also demonstrates how to retrieve details of the selected file or directory. It uses the Desktop class to display(/print/edit) files.

Not directly. What you will need to do:

  1. Store the file name (with path if you want to make your life easier) in the node. Extend DefaultMutableTreeNode for this.
  2. When a node is selected, read the file name from it.
  3. Use the usual Java IO API to read the file as text

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM