Hai,
I need to write a program that displays the files in a directory as a tree. I need to attach a popup menu with popupmenu item open that opens the file in wordpad or notepad on selecting it. I have done the display part. Can anyone help me in attaching the popup menu to each node of the tree and opening the files by selecting the menu item? Thanks in advance. My code is shown below:
import javax.swing.JFrame; import javax.swing.JLabel; import java.awt.BorderLayout; import java.awt.Desktop; import javax.swing.JTree; import javax.swing.tree.*; import java.io.File; import javax.swing.*; class FileTree { static private final String title = "FileTree GUI"; public static void main(String [] args) { // create a top-level container with a title JFrame frame = new JFrame(title); // set the size frame.setSize(200,400); // establish what happens when the window is closed // (without this the program would keep running!) frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // we want a flow layout manager frame.setLayout(new BorderLayout()); JTree t=new JTree(createTree("/home/Desktop")); frame.add(t,BorderLayout.CENTER); frame.setVisible(true); } // create the tree from the file system recursively. // This will take a long time if the directory name passed // in holds a lot of files (and subdirectories). static DefaultMutableTreeNode createTree(String dirname) { //static TreeModel createTree(String dirname) { File f = new File(dirname); DefaultMutableTreeNode top = new DefaultMutableTreeNode(); top.setUserObject(f.getName()); if (f.isDirectory() ) { System.out.println("Processing Directory " + f); File fls[] = f.listFiles(); for (int i=0;i<fls.length;i++) { top.insert(createTree(fls[i].getPath()),i); } } return(top); } }