Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Just as <a href="https://stackoverflow.com/users/491245/phoneixs">PhoenixS</a> has mentioned in his answer above, the procedure involves the following steps:</p> <ol> <li>Read the <code>.txt</code> file line by line. In this example we use <a href="http://docs.oracle.com/javase/6/docs/api/java/io/BufferedReader.html" rel="nofollow noreferrer">BufferedReader</a> in conjunction with <a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/io/InputStreamReader.html" rel="nofollow noreferrer">InputStreamReader</a> OR <a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/io/FileReader.html" rel="nofollow noreferrer">FileReader</a>, depending on whether we are reading the <code>.txt</code> file as a resource or from the File System respectively.</li> <li>Depending on the formatting of the <code>.txt</code> file, use the delimiter to determine the hierarchy level of each line as it is being read. Use this to create the relevant nodes of your <code>Jtree</code>. In this case the <code>.txt</code> file is tab-delimited, with the number of tabs indicating the hierarchy level. </li> </ol> <p>Here is the <code>TreeFromTextFile</code> class I wrote:</p> <pre><code>/* * File: TreeFromTextFile.java * Created 2013-02-06 * This Class creates a JTree using data from a Specially formatted text File. * The supplied text file is tab-delimited, as illustrated at: * https://stackoverflow.com/questions/14724014/create-jtree-using-data-from-text-file * * You can use either InputStreamReader to read thetext file as a resource * or use FileReader to read the text file from the file System */ import java.io.BufferedReader; //import java.io.FileReader; import java.io.InputStreamReader; import java.io.LineNumberReader; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeSelectionModel; /** * @author engineervix * */ public class TreeFromTextFile { private BufferedReader in; private LineNumberReader ln; private String line; //value of a line in the text file private String root; //value to be used for the root Node of our JTree private String filename = "TheTextFile.txt"; private String encoding = "UTF-8"; private DefaultMutableTreeNode top; private JTree tree; public TreeFromTextFile() { getRootNode(); top = new DefaultMutableTreeNode(root); createNodes(top); //Create a tree that allows one selection at a time. tree = new JTree(top); tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); } //this method reads the file and prints all the lines to standard output //for testing purposes public void readFile() { try { //in = new BufferedReader(new FileReader("Path\\To\\File.txt")); in = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(filename), encoding)); while ((line = in.readLine()) != null) { System.out.println(line); } in.close(); } catch (Exception e) { e.printStackTrace(); } } //this method reads the first line in the text file and assigns it //to the root variable which will be used for the root node of our JTree private void getRootNode() { try { //in = new BufferedReader(new FileReader("Path\\To\\File.txt")); in = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(filename), encoding)); ln = new LineNumberReader(in); if (ln.getLineNumber() == 0) { root = ln.readLine(); //System.out.println(root); } in.close(); } catch (Exception e) { e.printStackTrace(); } } /** * this method counts the number of occurrences of a given * &lt;code&gt;char&lt;/code&gt; in the Specified * &lt;code&gt;String&lt;/code&gt; source: * https://stackoverflow.com/questions/275944/how-do-i-count-the-number-of-occurrences-of-a-char-in-a-string */ private int countOccurrences(String haystack, char needle) { int count = 0; for (int i = 0; i &lt; haystack.length(); i++) { if (haystack.charAt(i) == needle) { count++; } } return count; } //create the Nodes private void createNodes(DefaultMutableTreeNode top) { DefaultMutableTreeNode category = null; // Level 1 in Hierarchy DefaultMutableTreeNode subCategory = null; // Level 2 in Hierarchy DefaultMutableTreeNode leaf = null; // Level 3 in Hierarchy try { //in = new BufferedReader(new FileReader("Path\\To\\File.txt")); in = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(filename), encoding)); while ((line = in.readLine()) != null) { if (countOccurrences(line, '\t') == 1) { category = new DefaultMutableTreeNode(line); top.add(category); } else if (countOccurrences(line, '\t') == 2) { subCategory = new DefaultMutableTreeNode(line); category.add(subCategory); } else if (countOccurrences(line, '\t') == 3) { leaf = new DefaultMutableTreeNode(line); subCategory.add(leaf); } //continue the else...if - if you have more levels } in.close(); } catch (Exception e) { e.printStackTrace(); } } public JTree getTree() { return tree; } } </code></pre> <p>To test the above, I wrote the <code>TreeFromTextFileDemo</code> class, which is as shown below:</p> <pre><code>/* * Requires TreeFromTextFile.java */ import java.awt.BorderLayout; import java.awt.Container; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTree; /** * @author engineervix */ public class TreeFromTextFileDemo { private static TreeFromTextFile tr = new TreeFromTextFile(); public static void main(String[] args) { JFrame frame = new JFrame("Demo | Creating JTree From File.txt"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container content = frame.getContentPane(); JTree t = tr.getTree(); content.add(new JScrollPane(t), BorderLayout.CENTER); frame.setSize(275, 300); frame.setLocationByPlatform(true); frame.setVisible(true); } } </code></pre> <hr> <p>The following screenshot illustrates the output. You can use <a href="http://pastebin.com/40dAucNS" rel="nofollow noreferrer">this file</a> as a sample text file.</p> <p><img src="https://i.stack.imgur.com/F48v8.jpg" alt="output of &lt;code&gt;TreeFromTextFileDemo.java&lt;/code&gt;"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload