简体   繁体   English

如何在 JTextArea 中显示 JTree 文件

[英]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.在我的程序中,我有一个JTree ,它由许多文件夹和文件填充。 When you select a file in the JTree I want that file to display in the JTextArea .当您在JTree选择一个文件时,我希望该文件显示在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 ?我不,您可以使用getLastSelectedPathComponent()JTree获取最后一个选定的项目,该项目可用于从树中获取所选文件,但这是树的一个节点......有没有办法从中获取文本这个节点喜欢使用FileReaderBufferReader吗?

I would put File objects in the tree and use a cell renderer to show (render) them as required.我会将File对象放在树中并使用单元渲染器根据需要显示(渲染)它们。

See File Browser GUI for an example.有关示例,请参阅文件浏览器 GUI The example also demonstrates how to retrieve details of the selected file or directory.该示例还演示了如何检索所选文件或目录的详细信息。 It uses the Desktop class to display(/print/edit) files.它使用Desktop类来显示(/打印/编辑)文件。

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.为此扩展DefaultMutableTreeNode
  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使用通常的 Java IO API 将文件读取为文本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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