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

Thread: Listing file names in a JList

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Listing file names in a JList

    I have a JList that will list certain files in a directory (may have seen an earlier post, same (JList). It works good, however, it lists the FULL path to the file, that's not what i want, i only want the last part of the full path, so i did this:

    String[] files;
     
    String Folder = "path";
    File fileFolder = new File(Folder);
    File[] fileList = fileFolder.listFiles();
    for(int i=0; i<=fileList.length; i++){
    	files[i] = file[i].toString();
    }

    But it doesn't work....


    If you can also tell me how to list certain file types (like .jar), that would be greatly appreciated.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,988
    My Mood
    Hungover
    Thanks
    133
    Thanked 557 Times in 478 Posts

    Default Re: Listing file names in a JList

    If you want help, I suggest you respond when you receive it. I'm talking about your problem with positioning/resizing your JList, which I tried to help you out on, but you seem to have abandoned. People will hesitate to help somebody who simply ignores advice or offers no feedback.

    That being said, I don't see any code that displays anything in a JList here. And what do you mean by "it doesn't work"? If you want help, you'll have to provide an SSCCE that demonstrates what you're doing.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Listing file names in a JList

    Quote Originally Posted by KevinWorkman View Post
    That being said, I don't see any code that displays anything in a JList here.
    this was just the code for getting the names of the files then storing them into an array. Then when the JList is initialized, it'll list the contents of that array.

    Looking back at this code, I dont see how it made sense to me. i tried using a different code here:

    String Folder = "Path";
    File jarsFolder = new File(Folder);
    File[] jarsList = jarsFolder.listFiles();
    for(int i=0; i <= jarsList.length; i++){
    	temp = jarsList[i].toString().split("\\");
    	jars[i] = temp[temp.length];
    		}

    attempting to split it by the \ character, which is then stored into a temporary array, then the very last string in the array would be passed into the next value of a permanent array, which the JList will use as its list. But the exception stack (to me) suggests that it doesn't like "\\" :

    Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
    \
    ^
    at java.util.regex.Pattern.error(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.util.regex.Pattern.<init>(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.lang.String.split(Unknown Source)
    at java.lang.String.split(Unknown Source)
    at b.<init>(b.java:70) <-- This is the line where the string is split
    at a.main(a.java:6)

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,988
    My Mood
    Hungover
    Thanks
    133
    Thanked 557 Times in 478 Posts

    Default Re: Listing file names in a JList

    Try:

    .split("\\\\")
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    KILL3RTACO (October 6th, 2011)

  6. #5
    Member
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Listing file names in a JList

    wow! +thanks.

    how would i split that result to get the extension? (silly question i know)

    EDIT: never mind, i figured out a way
    Last edited by KILL3RTACO; October 6th, 2011 at 08:34 PM.

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,988
    My Mood
    Hungover
    Thanks
    133
    Thanked 557 Times in 478 Posts

    Default Re: Listing file names in a JList

    I should also point out that my solution is not guaranteed to be portable- if you go to a different operating system with a different path separator (like '/'), you'll have to do something different.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #7
    Member
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Listing file names in a JList

    yeah, i know, like mac for instance. right now i'm focused for Windows, then i can try to see about others. the program uses console commands from windows. i would need to learn terminal commands from mac if i am to expand it to mac as well.

Similar Threads

  1. Issue with code. Does not detect duplicate names in file
    By suxen in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 29th, 2011, 01:13 AM
  2. [SOLVED] Array listing problem
    By saito1234 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 19th, 2011, 02:33 AM
  3. Listing files in a folder in a .jar
    By kulan8 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: December 22nd, 2010, 07:18 AM
  4. Listing the alphabet in lower and uppercase
    By Nemphiz in forum Object Oriented Programming
    Replies: 2
    Last Post: May 25th, 2010, 05:25 PM
  5. regular expressions, characters unallowed in file names
    By chopficaro in forum Java Theory & Questions
    Replies: 3
    Last Post: May 6th, 2010, 03:17 PM

Tags for this Thread