Pleaseeeeeeeeeeeeee help !!!!!!!!!!!!
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   swathi_narayanan
Posted On:   Monday, October 8, 2001 05:17 AM

Hi Iam woking in visual cafe 4.0. I have a tree sturcture ,when i click on the file.I want the conents to be displayed on JTextarea. Here is the code for JTree , please help me display the conents of the file in JTextarea : import javax.swing.*; import javax.swing.event.*; import javax.swing.tree.*; import java.awt.*; import java.awt.event.*; import java.io.File; import java.util.EventObject; public class Test extends JFrame { public Test() { final JTree tree = new JTree(createTreeModel()); JScrollPane scrollPane = new JScrollPane(tree); getContentPane().add(scrollPane, BorderLayout.CENTER); tree.addTreeExpans   More>>

Hi

Iam woking in visual cafe 4.0.
I have a tree sturcture ,when i click on the file.I want the conents to be displayed on JTextarea.

Here is the code for JTree , please help me display the conents of the file in JTextarea :
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.EventObject;

public class Test extends JFrame {
public Test() {
final JTree tree = new JTree(createTreeModel());
JScrollPane scrollPane = new JScrollPane(tree);

getContentPane().add(scrollPane, BorderLayout.CENTER);


tree.addTreeExpansionListener(new TreeExpansionListener(){
public void treeCollapsed(TreeExpansionEvent e) {
}
public void treeExpanded(TreeExpansionEvent e) {

TreePath path = e.getPath();
FileNode node = (FileNode)
path.getLastPathComponent();

if( ! node.isExplored()) {
DefaultTreeModel model = (DefaultTreeModel)tree.getModel();


node.explore();
model.nodeStructureChanged(node);
}
}

});
}
private DefaultTreeModel createTreeModel() {
File root = new File("C:/");
FileNode rootNode = new FileNode(root);

//rootNode.explore();
return new DefaultTreeModel(rootNode);
}
public static void main(String args[]) {


Test test1 = new Test();
test1.setSize(500,500);
test1.setVisible(true);




}
}
class FileNode extends DefaultMutableTreeNode {
private boolean explored = false;

public FileNode(File file) {
setUserObject(file);
}
public boolean getAllowsChildren() { return isDirectory(); }
public boolean isLeaf() { return !isDirectory(); }
public File getFile() { return (File)getUserObject(); }

public boolean isExplored() { return explored; }

public boolean isDirectory() {
File file = getFile();
return file.isDirectory();
}
public String toString() {
File file = (File)getUserObject();
String filename = file.toString();
//int index = filename.lastIndexOf(File.separator);

//return (index != -1 && index != filename.length()-1) ?
// filename.substring(index+1) :
// filename;

return filename;


}
public void explore() {
if(!isDirectory())
return;

if(!isExplored()) {
File file = getFile();
File[] children = file.listFiles();

for(int i=0; i < children.length; ++i)
add(new FileNode(children[i]));

explored = true;
}
}
}



Thanks a lot in advance
swathi

   <<Less
About | Sitemap | Contact