• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

showing a text file from a jtree selection path

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi im working on a program that when you add a folder into the jtree it show up all the files in it and it is save
automatically when the windows is closing with writeobject

but atm i have a problem with displaying the file when i click on a node in the jtree

i think it might be because of the treepath
atm its show me this [root, D:\\Word, D:\\Word\\abbb.txt]

i would like to be able just to show the " D:\\Word\\abbb.txt "
so when i call the bufferedReader File
it show up all the contents of the file in the jtextarea area


here is my code

i would need this big help
im new in jtree and im learning it by myself so ty in advance


here is my full code and the code in bold is where the problem is


 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The TreePath API docs indicate that TreePath doesn't do what you think it does: you're seeing a string representation of the entire path ending with your filename. If you were deeper in the tree you'd have more items.

If this is the route you're going to take you may want TreePath.html#getLastPathComponent(), which returns only the last component.
 
nic den
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cool im gonna work on it but ty for the help David
yeah i think i need to work with the getLastPathComponent function like you said
p.s. i have an another version of all this with the StyleDocument ( bold italic )
i might send most of the code that work out so you guys can see the result
if i still have trouble with this
im gonna reply

and ty for the help again
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
you are using String file = tp.toString(); which means ! show the TreePath tp as a string by using the toString method , i think you must override this toString method from the TreePath , you can make another class like MyTreePath extends Treepath and override the toString method there , you can use this MyTreepath instead of TreePath
 
Efstathios Arethas
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public String toString() {
StringBuffer tempSpot = new StringBuffer("[");

for(int counter = 0, maxCounter = getPathCount();counter < maxCounter;
counter++) {
if(counter > 0)
tempSpot.append(", ");
tempSpot.append(getPathComponent(counter));
}
tempSpot.append("]");
return tempSpot.toString();
}

it uses a loop , as you can see a StringBuffer which just adds this with the append method! just change it! By the way you should use StringBuffer too!
 
nic den
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the problem has been solved

import javax.swing.*;
import javax.swing.tree.*;

import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.io.*;

public class SimpleTree extends JFrame
{
private JTree tree;
private JTextArea text;
private DefaultTreeModel treeModel;
private DefaultMutableTreeNode rootNode;
private TreePath tp;
private File file;

public SimpleTree()
{
setLayout(new BorderLayout());

addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
writeObjectTreeModel();
System.exit(0);
}
});

readObjectTreeModel();

tree = new JTree(treeModel);
tree.setRootVisible(false);
add(new JScrollPane((JTree)tree),"Center");
text = new JTextArea(80,80);
add(text,"East");

tree.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
tp = tree.getSelectionPath();
MutableTreeNode selectNode = (MutableTreeNode) tp.getLastPathComponent();
String file = selectNode.toString();
if (tp != null)
{

String file1 = file.replaceAll("\\\\", "\\\\\\\\");
System.out.println(file1);
try
{
BufferedReader in = new BufferedReader(new FileReader(file1));
String str;

while ((str = in.readLine()) != null)
text.append(str);
in.close();
}
catch (IOException e)
{
}
}
}
});

setSize(800,800);
setVisible(true);
}


public void getList(DefaultMutableTreeNode node, File f)
{
if(!f.isDirectory())
{
DefaultMutableTreeNode folder = new DefaultMutableTreeNode(f);
node.add(folder);
}
else if(f.isDirectory())
{
DefaultMutableTreeNode child = new DefaultMutableTreeNode(f);
node.add(child);

File fList[] = f.listFiles();
for (int i = 0; i < fList.length; i++)
getList(child, fList[i]);
}
}

public void writeObjectTreeModel()
{
try
{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("treeModel.dat"));
oos.writeObject(treeModel);
oos.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}

public void readObjectTreeModel()
{
try
{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("treeModel.dat"));
treeModel = (DefaultTreeModel)ois.readObject();
rootNode = (DefaultMutableTreeNode)treeModel.getRoot();
String str = JOptionPane.showInputDialog(tree,"Enter New Project");
getList(rootNode, new File(str));
ois.close();
}
catch (Exception e)
{
e.printStackTrace();
rootNode = new DefaultMutableTreeNode("Sports");
treeModel = new DefaultTreeModel(rootNode);
}
}

public static void main(String s[])
{
try
{
UIManager.setLookAndFeel("com.sun.java." + "swing.plaf.motif.MotifLookAndFeel");
}
catch (Exception e)
{
e.printStackTrace();
}

SimpleTree frame = new SimpleTree();
}
}


ty for the help again david
 
nic den
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi efs
im gonna look at your code
it sound interesting
ty for your tip
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Efstathios Arethas wrote:you are using String file = tp.toString(); which means ! show the TreePath tp as a string by using the toString method , i think you must override this toString method from the TreePath , you can make another class like MyTreePath extends Treepath and override the toString method there , you can use this MyTreepath instead of TreePath


Why override JTree to provide something it already provides?
reply
    Bookmark Topic Watch Topic
  • New Topic