Results 1 to 1 of 1
  1. #1
    Angie2011 is offline Member
    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Default how do i Modify a program to display directory content in JList object

    trying to use JList object to read files in a directory.how can i add it change this program?

    Java Code:
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Exe6
    {
    public static void main(String []args)
    {
    JFrame aFrame = new JFrame("Directory Content");
    FCPanel x = new FCPanel();
    aFrame.getContentPane().add(x);
    aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    aFrame.pack();
    aFrame.setSize(500, 450);
    aFrame.setVisible(true);
    } // main
    } // MyDir4
    class FCPanel extends JPanel implements ActionListener  {
    String path;
    String []dirList;
    
    
    
    JTextField log = new JTextField(20);
    JTextArea view = new JTextArea(20, 20);
    JFileChooser fc = new JFileChooser();
    JButton pick = new JButton("Select a folder");
    JButton open = new JButton("Show folder");
    public FCPanel() {
    	
    	
    	
    	/*File folder = new File("c:/mydocument/");    
    	File [] dirList=folder.listFiles();         //initialize the JList  
    	JList list = new JList();         //set the listing of the directory as the data for the JList    
    	list.setListData();         //add the list to whatever container you need        ....
    	*/
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    JPanel p = new JPanel();
    p.add(pick);
    p.add(open);
    p.add(log);
    add(p, BorderLayout.PAGE_START);
    log.setEditable(false);
    pick.addActionListener(this);
    open.addActionListener(this);
    view.setEditable(false);
    JScrollPane sp = new JScrollPane(view);
    add(sp, BorderLayout.CENTER);
    
    
    } // constructor
    public void actionPerformed(ActionEvent e)
    {
    if (e.getSource() == pick)
    {
    int returnVal = fc.showOpenDialog(FCPanel.this);
    if (returnVal == JFileChooser.APPROVE_OPTION)
    {
    File file = fc.getSelectedFile();
    list();
    dirList = file.list();
    path = file.getName();
    log.setText(path);
    }
    } // pick
    if (e.getSource() == open)
    {
    view.setText("");
    for (int k=0; k < dirList.length; ++k)
    view.append(dirList[k] + "\n");
    } // open
    } // actionPerformed
    } // FCPanel
    Last edited by Angie2011; 11-14-2011 at 07:00 AM.

Similar Threads

  1. Modify content of a file on server using an applet
    By calnastic in forum New To Java
    Replies: 18
    Last Post: 07-04-2011, 02:45 AM
  2. Replies: 0
    Last Post: 12-09-2010, 05:56 PM
  3. Hyperlinking the content of a Jlist
    By mbarandao in forum Java Applets
    Replies: 15
    Last Post: 10-08-2010, 05:43 PM
  4. Replies: 2
    Last Post: 08-12-2010, 07:30 AM
  5. How do I modify the values in an object array?
    By ConMan in forum New To Java
    Replies: 1
    Last Post: 06-02-2010, 11:26 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
  •