1,105,447 Community Members

How can i modify this program to show directory contect using JList?

Member Avatar
angeriel1
Newbie Poster
7 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
Member Avatar
JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
1
 

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.

Member Avatar
angeriel1
Newbie Poster
7 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

Yes, that's where the list comes from

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: