-2

I am not sure if this was asked before. But anyway, I hope that the community could provide me with some guidance. I am creating a menu GUI using Netbean JFrame where I have this JLIST that I wish to populate with files from a specific directory. Then the user can select more than one items from the JList and generate a dashboard report from excel. May I know how can this be done? Thanks.

Okay just to not confuse anyone, I am stuck on trying to populate the JList with the file from a specific directories.

Let me clarify, I have read the other post from the forum. However I am unclear on how the program is linked to the list in the JFrame form. Is it by naming convention or I have to create another JList. Please I am really stuck and have spent hours searching the web for answers but still am stuck.

Zi Kai
  • 107
  • 9
  • 1
    Where are you stuck? Reading in the data? Putting data in a JList? Please ask a more specific question and show your code if possible. If you have absolutely no idea where to start, please have a look at Patricia Shanahan's excellent page as it will give you some ideas: [Starting Writing a Program](http://www.patriciashanahan.com/beginner.html), the key process being to break the big project down into small steps, and then solving each small step one at a time. – Hovercraft Full Of Eels Mar 07 '15 at 16:19
  • Then if you're still not successful, you can post a **much** better and more informative question, one that shows us your code and your precise thoughts on your problem, and one that likely would allow us to give much more specific and helpful suggestions. – Hovercraft Full Of Eels Mar 07 '15 at 16:19
  • `"Okay just to not confuse anyone, I am trying to populate the JList with the file from a specific directories."` -- this is a very broad requirement and not yet a specific question. Again, what have you tried, where are you stuck? Surely you've gone through tutorials on the specific parts of the problem, right? – Hovercraft Full Of Eels Mar 07 '15 at 16:21
  • 2
    Possible duplicate of [JList that contains the list of Files in a directory](http://stackoverflow.com/questions/7222161/jlist-that-contains-the-list-of-files-in-a-directory). Also look at [this site's search result](http://stackoverflow.com/search?q=%5Bjava%5D+jlist+files+directory). – Hovercraft Full Of Eels Mar 07 '15 at 16:23
  • This doesn't help me much. I am stuck on how to populate the Jlist with the files from a certain directory using Netbean JFrame. – Zi Kai Mar 07 '15 at 16:25
  • 2
    What do you mean it doesn't help much, it shows exactly how to do this for Java Swing GUI's, and that's all that your "NetBeans JFrame" is. – Hovercraft Full Of Eels Mar 07 '15 at 16:26
  • 2
    `This doesn't help me much.` - how does it not help? How could you have possibly read the answer, downloaded the code, tested the code and reply in the forum in less that 2 minutes??? – camickr Mar 07 '15 at 16:29
  • Because I read the answer before I ask. I am not sure how it links to the JList in the JFrame. – Zi Kai Mar 07 '15 at 16:36
  • The problem is that you're using NetBeans to generate code, but don't yet understand how the Swing library works, which NetBeans is using, and so don't yet understand what you're reading in the answers already posted on this site. While we can't teach you the basics of Swing as it's beyond the limits of this site, you can teach yourself, and so your best bet is to go to the Swing tutorials which you can find here: [Swing Info](http://stackoverflow.com/questions/tagged/swing). – Hovercraft Full Of Eels Mar 07 '15 at 16:48
  • *"I am not sure how it links to the JList in the JFrame."* Are you talking about [this answer](http://stackoverflow.com/a/7222943/418556) which puts a **`JList` of `File` objects in a `JFrame`?** If you cannot figure out how it 'links' to your attempt, I'd seriously question if you've got what it takes to do programming.. – Andrew Thompson Mar 07 '15 at 16:48
  • Thanks Hovercraft, I shall go read up abit on JFrame. And Thompson, I am still new to Netbean JFrame. First day playing with it. – Zi Kai Mar 07 '15 at 17:11

1 Answers1

2

Anyway I got it. Below is how it should be done.

    DefaultListModel model1 = new DefaultListModel();
    File o = new File("c:/");

    File[] yourFileList = o.listFiles();
    for(File f : yourFileList) {
        model1.addElement(f.getName());
    }
    jList1.setModel(model1);
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Zi Kai
  • 107
  • 9