Hi everyone! i'm new to this forum...and i'm really noob..i need some help. i want to create a program that lists all folders in my computer, and then if user clicks on the folder name, the list of files in the folder will pop out. please refer to my drawing of the idea in attachments :)

what do you suggest i do? i have no idea..so if someone can give me some idea that would be very helpful to me..i am not very versed in swing, although i know jsp and servlet.

please help and thanks! :)

04-21-2011, 03:28 AM

sunde887

Do you mean, graphically how do you display a picture of a folder? If so you can use image icons and buttons or mouse listeners. Do you mean more of how do you find the computers whole file directory?

04-21-2011, 03:37 AM

nashbender

the folders should be displayed in button form..user clicks the button and the contents of the folder will be at displayed at right side. the file directory i have the non gui code, using java.io.file..

public void iterate(File folder) {
ArrayList<File> folders = getFolders(folder);
for (int i = 0; i < folders.size(); i++) {
System.out.println(folders.get(i));
// we add the subfolders of the current folder to the main FOLDERS array list
folders.addAll(getFolders(folders.get(i)));
// then we remove the current folder, so that we dont read it twice
folders.remove(i);
}
}

but can you give me some hints as to how can i duplicate the buttons to each individual folder?
do i use for loop?

04-21-2011, 06:37 AM

sunde887

If you have a string representation of each folder you can create an array of JButtons x long, where x is the amount of folders(an array list may be better), and then you can instantiate and add a jbutton for each folder. This code probably won't be easy if you are fairly new to swing.

04-21-2011, 08:10 AM

nashbender

ok i will try to do all that. i agree it is hard for me as a beginner. maybe if i turn the buttons into a JTree would it be easier? what do you recommend?

04-22-2011, 04:16 AM

nashbender

can anybody else help me? :confused:

04-22-2011, 05:28 AM

nashbender

Here's what i've done so far..i have tried creating a frame with 2 panels and tested some buttons on them..now i want to integrate my folder code with that one. trouble is now i have a 'null pointer exception'..i know i made a mistake but cannot pinpoint what it is..can you kind peoples help me please?

ArrayList<File> folders = getFolders(file);
for (int i = 0; i < folders.size(); i++) {
panel.add(new JButton(String.valueOf(folders.get(i))), gbc);
// we add the subfolders of the current folder to the main FOLDERS array list
folders.addAll(getFolders(folders.get(i)));
// then we remove the current folder, so that we dont read it twice
folders.remove(i);
}
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setPreferredSize(d);
return scrollPane;
}