Results 1 to 5 of 5
- 12-26-2011, 09:15 PM #1
Listing files WITHOUT path names...and not as strings! (SOLVED)
So...I have this piece of code...
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!)Java Code:File leftDir=new File("/Volumes/Apple Scruffs/"); File[] leftDirList=leftDir.listFiles(); listOnLeft.setListData(leftDirList); listLeft.getViewport().add(listOnLeft);
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.
- 12-26-2011, 11:04 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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.
(or simple, call --> leftDir.list() instead of listFiles() :D)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 you can write your own renderer for the jlist or use the FilenameFilter or or or :)
- 12-26-2011, 11:32 PM #3
Re: Listing files WITHOUT path names...and not as strings!
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.
- 12-26-2011, 11:36 PM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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; } });
-
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)
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();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; }
Similar Threads
-
listing files of directory and subdirectory
By oulutas in forum New To JavaReplies: 10Last Post: 10-10-2010, 05:48 PM -
listing files at the FTP
By oulutas in forum New To JavaReplies: 6Last Post: 10-10-2010, 11:30 AM -
comparing strings in notebook to names of a file
By debz in forum New To JavaReplies: 8Last Post: 02-20-2009, 12:40 PM -
Listing subdirectories/files with filter
By Java Tip in forum Java TipReplies: 0Last Post: 01-13-2008, 07:20 AM -
Listing subdirectories/files
By Java Tip in forum Java TipReplies: 0Last Post: 01-13-2008, 07:19 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks