how can I use JList object in place of JFilechooser show the directory content.any tips or somthing to help me understand this will be apreciated.

// Display directory content in a JTextArea object.
      // The user selects a directory via JFileChooser object.
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyDir4
{
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() {
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();
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

At line 50 you have retrieved an array containing the file names.
JList has a setListData method that takes an array as a parameter and displays the contents of the array.
So you just need to create a JList somewhere in your window and call its setListData method passing the array of file names.

At line 50 you have retrieved an array containing the file names.
JList has a setListData method that takes an array as a parameter and displays the contents of the array.
So you just need to create a JList somewhere in your window and call its setListData method passing the array of file names.

OH Ok!! I just need clarity on this, do I Still keep the Jfilechooser right?

Yes, that's where the list comes from

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.