Results 1 to 13 of 13
  1. #1
    nashbender is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Red face how to display computer folders and files in GUI

    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! :)
    Attached Thumbnails Attached Thumbnails how to display computer folders and files in GUI-draftpicture.jpg  

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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?

  3. #3
    nashbender is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

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

    Java Code:
    import java.io.File;
    import java.io.FileFilter;
    import java.util.ArrayList;
    
    public class CreateDirectory {
    
    	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);
    		}
    	}
    
    	private ArrayList<File> getFolders(File folder) {
    		ArrayList<File> listAr = new ArrayList<File>();
                    // uses FileFilter to just pull out directories, then puts in an array
    		File[] list = folder.listFiles(new FileFilter() {
    			
    			public boolean accept(File file) {
    				if (file.isDirectory())
    					return true;
    				return false;
    			}
    		});
    
                    // puts all the items from the array in an arraylist
    		for (int i = 0; i < list.length; i++) 
    			listAr.add(list[i]);
    		
    
    		return listAr;
    	}
    	
    	public static void main(String[] args) {
    		
    		CreateDirectory myFolderList = new CreateDirectory(); 
    		File File = new File("/");
    		myFolderList.iterate(File);
    		
    	}
    
    }
    thanks for the swift reply :)

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Do you understand how to write action listeners? You can display the folders as buttons and when you click a button it can display the contents on a new jpanel.

    Others may have better advice if this isn't what you are looking for.

  5. #5
    nashbender is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    um, can you show example code for putting my original display folder code as buttons? i don't understand how to do that...tq

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,301
    Rep Power
    20

    Default

    Why reinvent JFileChooser?

    db

  7. #7
    nashbender is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    haha my boss asked me to do this..maybe to show my java skill i guess.. mainly just to see the content of folders rather than selecting them.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The best place to start would be the oracle tutorials.
    Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

    Here is a very simple example(very simple)
    Java Code:
    public class Test{
      private JFrame frame;
      private JPanel panel;
      private JButton  button1;
    
      private class ButtonChange implements ActionListener{
        public void actionPerformed(ActionEvent e){
          button1.setText("ABC"); 
        }
      }
     
      public Test(){
        frame = new JFrame("Test");
        panel = new JPanel();
        panel.setLayout(new BorderLayout());
        button1 = new JButton("File"); //you can also have an image icon as an additional argument
        button1.addActionListener(new ButtonChange());
        panel.add(button1, BorderLayout.WEST);
        panel.add(new JLabel("HELLO"), BorderLayout.EAST);
        frame.add(panel);
        frame.setSize(400, 400);
        frame.setVisible(true);
      }
      public static void main(String[] args){
        Test t = new Text();
      }
    }

  9. #9
    nashbender is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    thanks for your guidance..

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

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  11. #11
    nashbender is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    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?

  12. #12
    nashbender is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Wink

    can anybody else help me? :confused:

  13. #13
    nashbender is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    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?

    here's the 2 panel code:
    Java Code:
    import java.awt.*;
    
    import javax.swing.*;
    
    public class Trypanel extends JFrame {
    private JPanel panel;
    private JScrollPane getContent() 
    {
    	Dimension d = new Dimension(200,400);
    	panel = new JPanel(new GridBagLayout());
    	GridBagConstraints gbc = new GridBagConstraints();
    	gbc.weightx = 1.0;
    	gbc.fill = GridBagConstraints.VERTICAL;
    	gbc.anchor = GridBagConstraints.WEST;
        panel.add(getPanel("Hi", d,  6, Color.red), gbc);
        panel.add(getPanel("Hello", d, 6, Color.green.darker()), gbc);
        return new JScrollPane(panel);
    }
    
    private JScrollPane getPanel(String header, Dimension d, int rows, Color color) {
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBackground(color);
        GridBagConstraints gbc= new GridBagConstraints();
        gbc.insets = new Insets(10,5,10,5);
        gbc.weightx = 1.0;
        gbc.gridwidth = GridBagConstraints.RELATIVE;
        JTextArea t = new JTextArea(header, 1, 5);
        panel.add(t);
        for(int i = 0, j = 1; i < rows; i++) {
            gbc.gridwidth = GridBagConstraints.RELATIVE;
            panel.add(new JButton(String.valueOf(j++)), gbc);
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            panel.add(new JButton(String.valueOf(j++)), gbc);
        }
        JScrollPane scrollPane = new JScrollPane(panel);
        scrollPane.setPreferredSize(d);
        return scrollPane;
    }
    
    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setTitle("Welcome to folders directory listing!");
        f.setLayout(new BorderLayout());
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new Trypanel().getContent());
        f.setSize(425,400);
        f.setLocation(200,200);
        f.setVisible(true);
    }
    }
    here is my current code:

    Java Code:
    import java.awt.*;
    import java.io.File;
    import java.io.FileFilter;
    import java.util.ArrayList;
    
    import javax.swing.*;
    
    public class Trypanel extends JFrame {
    private JPanel panel;
    private File file;
    private JScrollPane getContent() 
    {
    	Dimension d = new Dimension(200,400);
    	panel = new JPanel(new GridBagLayout());
    	GridBagConstraints gbc = new GridBagConstraints();
    	gbc.weightx = 1.0;
    	gbc.fill = GridBagConstraints.VERTICAL;
    	gbc.anchor = GridBagConstraints.WEST;
        panel.add(getPanel("Hi", d,  Color.red), gbc);
        return new JScrollPane(panel);
    }
    
    private JScrollPane getPanel(String header, Dimension d, Color color) {
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBackground(color);
        GridBagConstraints gbc= new GridBagConstraints();
        gbc.insets = new Insets(10,5,10,5);
        gbc.weightx = 1.0;
        gbc.gridwidth = GridBagConstraints.RELATIVE;
        JTextArea t = new JTextArea(header, 1, 5);
        panel.add(t);
        
        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;
    }
    
    
    
    	private ArrayList<File> getFolders(File folder) {
    		ArrayList<File> listAr = new ArrayList<File>();
                    // uses FileFilter to just pull out directories, then puts in an array
    		File[] list = folder.listFiles(new FileFilter() {
    			
    			public boolean accept(File file) {
    				if (file.isDirectory())
    					return true;
    				return false;
    			}
    		});
    
                    // puts all the items from the array in an arraylist
    		for (int i = 0; i < list.length; i++) 
    			listAr.add(list[i]);
    		
    
    		return listAr;
    	}
    	
    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setTitle("Welcome to folders directory listing!");
        f.setLayout(new BorderLayout());
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new Trypanel().getContent());
        f.setSize(425,400);
        f.setLocation(200,200);
        f.setVisible(true);
    }
    }
    please? i dunno what else to tweak :confused:

Similar Threads

  1. Folders to group source files
    By Skiller in forum New To Java
    Replies: 6
    Last Post: 03-31-2011, 08:20 PM
  2. Searching directories for folders and .txt files
    By XDrew574X in forum New To Java
    Replies: 1
    Last Post: 03-29-2011, 09:41 PM
  3. How to get all Sharing folders in LAN...?
    By ram07 in forum Networking
    Replies: 0
    Last Post: 03-27-2011, 02:36 AM
  4. Replies: 4
    Last Post: 10-10-2009, 12:26 AM
  5. Replies: 20
    Last Post: 12-17-2007, 03: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
  •