Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: display files in a folder as a tree, attach a popup menu to the nodes

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post display files in a folder as a tree, attach a popup menu to the nodes

    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);
      }
     
    }
    Last edited by rekharaj; February 22nd, 2012 at 02:35 PM.


  2. #2
    Member
    Join Date
    Feb 2012
    Posts
    58
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: display files in a folder as a tree, attach a popup menu to the nodes

    I would suggest you these steps to accomplish your goal:
    - create a popup menu with menu items.
    - add a listener to mouse event of the tree.
    - check if the event fired is right mouse clicked, show the popup menu.

Similar Threads

  1. How to test all files in a folder
    By GodspeedPR in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 28th, 2011, 07:15 AM
  2. Seraching through files in a folder for a pattern match inside the files.
    By dazzabiggs in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 2nd, 2011, 09:35 AM
  3. Replies: 2
    Last Post: January 21st, 2011, 02:07 AM
  4. popup menu
    By antonio69 in forum AWT / Java Swing
    Replies: 1
    Last Post: May 20th, 2010, 05:24 AM
  5. Replies: 1
    Last Post: October 23rd, 2009, 04:17 PM

Tags for this Thread