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 4 of 4

Thread: JTree - file system viewer tree only with fixed file extensions

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Location
    Poland
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post JTree - file system viewer tree only with fixed file extensions

    Hello,
    I'm in the process of writing a school project and I have one problem i can't solve.
    I have a tree that shows directories and file paths but I need to display only .mp3 and .wav files.
    I have a class with a tree model:
    import java.io.File;
    import java.util.Iterator;
    import java.util.Vector;
     
    import javax.swing.JTextArea;
    import javax.swing.event.TreeModelEvent;
    import javax.swing.event.TreeModelListener;
    import javax.swing.tree.TreeModel;
    import javax.swing.tree.TreePath;
     
    class FileSystemModel implements TreeModel {
    	  private File root;
    	  private JTextArea fileDetailsTextArea = new JTextArea();
    	  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();
    	    }
    	  }
     
    	  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: " + (double)(file.length()/1024)/1024 + " MB" + "\n");
    		    return buffer.toString();
    		  }
    	}

    Can someone give me some advice how to do that?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: JTree - file system viewer tree only with fixed file extensions

    I need to display only .mp3 and .wav files.
    Somewhere you could add a filter that only returns the files with the desired extensions. There are File class methods that work with filters.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    brozinho (June 20th, 2014)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: JTree - file system viewer tree only with fixed file extensions

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    I haven't studied your code, but in general you should be able to filter the files to the ones desired using the file's extension. Review the File and String classes for the available options.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    brozinho (June 20th, 2014)

  6. #4
    Junior Member
    Join Date
    Jun 2014
    Location
    Poland
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JTree - file system viewer tree only with fixed file extensions

    You are the best guys, thank u very much for fast answers.

Similar Threads

  1. Viewing Website File Tree
    By aussiemcgr in forum Java Theory & Questions
    Replies: 4
    Last Post: April 25th, 2014, 12:57 PM
  2. Format difference in System.out Vs. file output
    By mwr76 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 10th, 2011, 09:13 PM
  3. Obtaining File System in Use
    By ebm1991 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: June 9th, 2011, 09:20 AM
  4. How to Use JTree to create a File System Viewer Tree
    By JavaPF in forum Java Swing Tutorials
    Replies: 0
    Last Post: March 15th, 2011, 05:05 AM
  5. How to Use JTree to create a File System Viewer Tree
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: March 15th, 2011, 05:05 AM

Tags for this Thread