0

I am new to Java Swing and create GUIs using Netbeans. I want to do the following:

  1. Allow the user to click a button to set the working directory (complete)
  2. When the directory is set, use a Jtree to display the contents like which will contain subfolders with lots of text files within them (complete).
  3. When i click on any text file in the Jtree, I want to be able to display the contents of the text files in a large uneditable text area.

I can display the directories in the Jtree and see the files, but i do not know create the dynamic behavior i need to view the files when clicked in the tree. Open to suggestions on how implement this most efficiently. Grateful for any help!

D Chase
  • 85
  • 7
  • You need to 1) divide and conquer, meaning separate your problem into separate smaller concerns and solve each one, in isolation from the other. For instance, creating the JTree and responding to selection has nothing to do with reading a text file and has nothing to do with displaying a text file. 2) Once you've done this, ask a much more specific answerable question about just what it is you're trying to solve, and 3) show a small but complete program, a [mcve] that compiles, runs and shows us your problem. – Hovercraft Full Of Eels Dec 08 '17 at 22:22
  • Read the section from the Swing tutorial on [How to Use Trees](https://docs.oracle.com/javase/tutorial/uiswing/components/tree.html). The `TreeIcon2Demo` does exactly what you want. – camickr Dec 08 '17 at 22:27
  • For loading the text file, see [`JTextComponent.read(Reader, Object)`](https://docs.oracle.com/javase/8/docs/api/javax/swing/text/JTextComponent.html#read-java.io.Reader-java.lang.Object-). – Andrew Thompson Dec 09 '17 at 04:30
  • Thanks @HovercraftFullOfEels. I am breaking my problem down to take smaller steps. So my first task i want to accomplish is to set the Tree directory based on the user selecting the "working directory" through a JFIleChooser. I have that working to the point of needing to set that directory in the Tree and refresh it. Not sure how to do that. – D Chase Dec 14 '17 at 21:11
  • Thanks @camickr I have downloaded that project and am looking at that. – D Chase Dec 14 '17 at 21:13
  • Thanks @AndrewThompson. I have tried that but was getting "file not found" and i believe it was due to not knowing the path of the file. Is there a way to use the path of the file when doing the read operation? – D Chase Dec 14 '17 at 21:15
  • You'll need to ask a new question to pursue that. This one was too broad in any case. But 2 tips. 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Make sure the tree nodes are holding **`File`** objects as opposed to anything else (like `String` objects giving the path of files. See the [File Browser GUI](http://codereview.stackexchange.com/q/4446/7784) for display of files in a tree. – Andrew Thompson Dec 14 '17 at 22:21

0 Answers0