Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2009
    Posts
    1
    Rep Power
    0

    Question Populating a gui list box with the files in a directory

    I need to populate a list box with the names of a directory files.
    For example
    I have a folder called x. In that folder there is the following files
    1.csv
    2.csv
    3.csv

    Now I need to populate the my gui list box to populate fields with
    1.csv
    2.csv
    3.csv

    Here is my code so far
    Public class dir{
    File folder = new File("c:/mydocument/");
    File [] listofFiles=folder.listFiles();
    }

    Now that I have the lisofFiles
    how do i pass the values to my main gui class

    An example code would be greatly apreciated.
    }

  2. #2
    []?[] is offline Member
    Join Date
    Aug 2009
    Posts
    1
    Rep Power
    0

    Default

    You are correct in the listing of the files. The following solution assumes that a GUI list box is a JList.

    If you look under the Java API for JList you will find a method called setListData... this is the method you are looking for.

    The following is a quick preview of what you code should look like
    Java Code:
    public class dir
    {
        public dir()
        {
            File folder = new File("c:/mydocument/");
            File [] listofFiles=folder.listFiles();
    
            //initialize the JList
            JList list = new JList();
    
            //set the listing of the directory as the data for the JList
            list.setListData(listofFiles);
    
            //add the list to whatever container you need
            ....
        }
    }

Similar Threads

  1. Populating JTree With Directory Info
    By SilentCodingOne in forum AWT / Swing
    Replies: 0
    Last Post: 07-07-2009, 02:37 PM
  2. (Recursively?) Getting Files in a Directory
    By Singing Boyo in forum New To Java
    Replies: 2
    Last Post: 05-21-2009, 08:37 AM
  3. Populating a drop down list from a database
    By matpj in forum New To Java
    Replies: 0
    Last Post: 01-19-2009, 12:14 PM
  4. how to list certain files in a given directory in java
    By corpusluteum in forum New To Java
    Replies: 6
    Last Post: 09-24-2008, 12:19 AM
  5. How can I get list of files in a directory
    By karma in forum New To Java
    Replies: 2
    Last Post: 12-14-2007, 11:20 PM

Tags for this Thread

Posting Permissions

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