Results 1 to 5 of 5
  1. #1
    nananya is offline Member
    Join Date
    Dec 2011
    Posts
    20
    Rep Power
    0

    Exclamation Question about making folder directory and explorer...

    I am trying to make these in java applet...and i had found how to make directory but how to display the selected folder's file in another frame?
    Just like this:
    Question about making folder directory and explorer...-eg.jpg
    And how to display them in different icon?
    like movie file display with player icon,audio file in sound wave icon...etc.

    I am using this code for the directory:
    File folder Tree with icons : Tree**Swing JFC**Java

    I hope there is anyone can help me...
    Thank you.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Question about making folder directory and explorer...

    Please post your code in the forum with code tags, wrap your code with[code]<YOUR CODE HERE>[/code] to get highlighting

  3. #3
    nananya is offline Member
    Join Date
    Dec 2011
    Posts
    20
    Rep Power
    0

    Default Re: Question about making folder directory and explorer...

    Here is my code until now:

    Java Code:
    import java.io.File;
    import java.util.Iterator;
    import java.util.Vector;
    
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JSplitPane;
    import javax.swing.JTextArea;
    import javax.swing.JTree;
    import javax.swing.event.TreeModelEvent;
    import javax.swing.event.TreeModelListener;
    import javax.swing.event.TreeSelectionEvent;
    import javax.swing.event.TreeSelectionListener;
    import javax.swing.tree.TreeModel;
    import javax.swing.tree.TreePath;
    
    public class FileTreeFrame extends JFrame {
      private JTree fileTree;
    
      private FileSystemModel fileSystemModel;
    
      private JTextArea fileDetailsTextArea = new JTextArea();
    
      public FileTreeFrame(String directory) {
        super("JTree FileSystem Viewer");
        fileDetailsTextArea.setEditable(false);
        fileSystemModel = new FileSystemModel(new File(directory));
        fileTree = new JTree(fileSystemModel);
        fileTree.setEditable(true);
        fileTree.addTreeSelectionListener(new TreeSelectionListener() {
          public void valueChanged(TreeSelectionEvent event) {
            File file = (File) fileTree.getLastSelectedPathComponent();
            fileDetailsTextArea.setText(getFileDetails(file));
          }
        });
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, new JScrollPane(
            fileTree), new JScrollPane(fileDetailsTextArea));
        getContentPane().add(splitPane);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(640, 480);
        setVisible(true);
      }
    
      private String getFileDetails(File file) {
        if (file == null)
          return "";
        StringBuffer buffer = new StringBuffer();
        buffer.append("Name: " + file.getName() + "\n");
        buffer.append("Path: " + file.getPath() + "\n");
        buffer.append("Size: " + file.length() + "\n");
        return buffer.toString();
      }
    
      public static void main(String args[]) {
        new FileTreeFrame("c:\\");
      }
    }
    
    class FileSystemModel implements TreeModel {
      private File root;
    
      private Vector listeners = new Vector();
    
      public FileSystemModel(File rootDirectory) {
        root = rootDirectory;
      }
    
      public Object getRoot() {
        return root;
      }
    
      public Object getChild(Object parent, int index) {
        File directory = (File) parent;
        String[] children = directory.list();
        return new TreeFile(directory, children[index]);
      }
    
      public int getChildCount(Object parent) {
        File file = (File) parent;
        if (file.isDirectory()) {
          String[] fileList = file.list();
          if (fileList != null)
            return file.list().length;
        }
        return 0;
      }
    
      public boolean isLeaf(Object node) {
        File file = (File) node;
        return file.isFile();
      }
    
      public int getIndexOfChild(Object parent, Object child) {
        File directory = (File) parent;
        File file = (File) child;
        String[] children = directory.list();
        for (int i = 0; i < children.length; i++) {
          if (file.getName().equals(children[i])) {
            return i;
          }
        }
        return -1;
    
      }
    
      public void valueForPathChanged(TreePath path, Object value) {
        File oldFile = (File) path.getLastPathComponent();
        String fileParentPath = oldFile.getParent();
        String newFileName = (String) value;
        File targetFile = new File(fileParentPath, newFileName);
        oldFile.renameTo(targetFile);
        File parent = new File(fileParentPath);
        int[] changedChildrenIndices = { getIndexOfChild(parent, targetFile) };
        Object[] changedChildren = { targetFile };
        fireTreeNodesChanged(path.getParentPath(), changedChildrenIndices, changedChildren);
    
      }
    
      private void fireTreeNodesChanged(TreePath parentPath, int[] indices, Object[] children) {
        TreeModelEvent event = new TreeModelEvent(this, parentPath, indices, children);
        Iterator iterator = listeners.iterator();
        TreeModelListener listener = null;
        while (iterator.hasNext()) {
          listener = (TreeModelListener) iterator.next();
          listener.treeNodesChanged(event);
        }
      }
    
      public void addTreeModelListener(TreeModelListener listener) {
        listeners.add(listener);
      }
    
      public void removeTreeModelListener(TreeModelListener listener) {
        listeners.remove(listener);
      }
    
      private class TreeFile extends File {
        public TreeFile(File parent, String child) {
          super(parent, child);
        }
    
        public String toString() {
          return getName();
        }
      }
    }

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Question about making folder directory and explorer...

    The code you linked to shows how to use different icons in the tree view.

    If you do not want to supply your own icons (as that code does) have a look at the FileSystemView class.

  5. #5
    nananya is offline Member
    Join Date
    Dec 2011
    Posts
    20
    Rep Power
    0

    Default Re: Question about making folder directory and explorer...

    ya...I didn't use icon for my directory but i wish to use icon on the file inside the folder...
    and my question is how to transfer the file in the folder to show in another frame and with icon...
    Anyway, thanks for your reply.

Similar Threads

  1. Making a method exercise question
    By katiebear128 in forum New To Java
    Replies: 6
    Last Post: 10-01-2011, 08:48 PM
  2. Swing question: Making a visual tic tac toe
    By alacn in forum New To Java
    Replies: 8
    Last Post: 07-05-2010, 02:42 AM
  3. Issure with folder (directory)
    By solris in forum New To Java
    Replies: 7
    Last Post: 06-30-2009, 05:03 AM
  4. opening directory (folder) to reveal contents.
    By solris in forum New To Java
    Replies: 6
    Last Post: 06-28-2009, 06:40 PM
  5. Replies: 15
    Last Post: 03-18-2009, 10:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •