Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By eRaaaa

Thread: Listing files WITHOUT path names...and not as strings!

  1. #1
    dauber's Avatar
    dauber is offline Member
    Join Date
    Dec 2011
    Location
    Chicago
    Posts
    3
    Rep Power
    0

    Default Listing files WITHOUT path names...and not as strings! (SOLVED)

    So...I have this piece of code...

    Java Code:
    File leftDir=new File("/Volumes/Apple Scruffs/");
    File[] leftDirList=leftDir.listFiles();
    listOnLeft.setListData(leftDirList);
    listLeft.getViewport().add(listOnLeft);
    This is all on the left side of a JSplitPane. listLeft is the JScrollPane on the left,and listOnLeft is a JList that's displayed in the "listLeft" pane. (I know, I need to rename those variables to make 'em less confusing!)

    Basically, this shows a list of all the files within /Volumes/Apple Scruffs -- and it works perfect...except that I just want the file names, not the paths. I did some searching, but all I could find were examples that specifically dealt with strings rather than components, and they weren't very helpful in isolating the filenames, either.

    Thoughts??
    Last edited by dauber; 12-26-2011 at 11:38 PM.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Listing files WITHOUT path names...and not as strings!

    There are many ways to get this. One of them would be, that you create a String array in addition to your File array and iterate over your file array and call the getName() method on each file and put it into your strng array:
    e.g.
    Java Code:
    		String[] names = new String[leftDirList.length];
    		for (int i = 0; i < leftDirList.length; i++) {
    			names[i] = leftDirList[i].getName();
    		}
    		list.setListData(names);
    (or simple, call --> leftDir.list() instead of listFiles() :D)

    Or you can write your own renderer for the jlist or use the FilenameFilter or or or :)
    dauber likes this.

  3. #3
    dauber's Avatar
    dauber is offline Member
    Join Date
    Dec 2011
    Location
    Chicago
    Posts
    3
    Rep Power
    0

    Default Re: Listing files WITHOUT path names...and not as strings!

    Quote Originally Posted by eRaaaa View Post
    (or simple, call --> leftDir.list() instead of listFiles() :D)

    Or you can write your own renderer for the jlist or use the FilenameFilter or or or :)

    Thanks for the reply, eRaaaa! leftDir.list() doesn't work -- this is a File array, remember, and leftDir.list() returns a String.

    I knew about FilenameFilter, but can't for the life of me figure it out. :(

    Your previous suggestion with the string code, though, DID IT. Thank you so much!
    Last edited by dauber; 12-26-2011 at 11:37 PM.

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Listing files WITHOUT path names...and not as strings!

    Yeah it returns a String array, but i dont understand whats wrong with that? Is that not what you want? (ok you lost some informations...)
    Try
    Java Code:
    listOnLeft.setCellRenderer(new ListCellRenderer() {
    			JLabel label = new JLabel();
    			@Override
    			public Component getListCellRendererComponent(JList list, Object value,
    					int index, boolean isSelected, boolean cellHasFocus) {
    				label.setText(((File)value).getName()); //instead of toString method of file class
    //TODO: isSelected - change color and so on...
    				return label;
    			}
    		});

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Listing files WITHOUT path names...and not as strings!

    Search a directory, find all the files, then get the ones you want (ending with *.txt, *.jpg, etc)

    Java Code:
        public static List<File> searchDirectory(String subdir, String fileext) {
    
            //a list to hold the files that you want
            List<File> foundFiles = new ArrayList<File>();
            //a list to hold all files found
            List<File> listOfFiles = new ArrayList<File>();
    
            File folder = new File(subdir);
            if (folder != null) {
    
                //add all Files in the folder to the list
                listOfFiles.addAll(java.util.Arrays.asList(folder.listFiles()));
    
                //loop through the Files
                for (int i=0; i<listOfFiles.size(); i++) {
    
                    if (listOfFiles.get(i).isFile()) {
                        String filename = listOfFiles.get(i).getName();
    
                        //add the files we are looking for to the other list
                        if (filename.endsWith(fileext)) {
                            foundFiles.add(listOfFiles.get(i));
                        }
    
                    }
    
                }
    
            }
            return foundFiles;
        }
    With your new list of files, just loop through them and use File f.getName() to get the name without the path. To get the full name with the path use f.getAbsolutePath();

Similar Threads

  1. listing files of directory and subdirectory
    By oulutas in forum New To Java
    Replies: 10
    Last Post: 10-10-2010, 05:48 PM
  2. listing files at the FTP
    By oulutas in forum New To Java
    Replies: 6
    Last Post: 10-10-2010, 11:30 AM
  3. Replies: 8
    Last Post: 02-20-2009, 12:40 PM
  4. Listing subdirectories/files with filter
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 07:20 AM
  5. Listing subdirectories/files
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 07:19 AM

Posting Permissions

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