how to display all the files that are selected using JFile chooser
please hepl me i m posting my code here.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class browser extends JFrame
{
String curDir;
JLabel statusbar;
public browser()
{
super("File Chooser Test Frame");
setSize(1050,600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
curDir = System.getProperty("user.dir") + File.separator;
statusbar = new JLabel("Output of your button choice goes here!");
Container c = getContentPane();
c.setLayout( null );
JButton openButton = new JButton("Browse file");
JButton dirButton = new JButton("Pick Dir");
JButton resetButton = new JButton("Reset");
JButton submitButton = new JButton("Submit");
final JTextArea tf = new JTextArea(100,50);
final JList l1 = new JList("list");
tf.setEditable(true);
// add the open FILE chooser
openButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
JFileChooser chooser = new JFileChooser(curDir);
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setMultiSelectionEnabled(true);
//Java 1.6 can use FileNameExtensionFilter class
//FileFilter filter=new FileNameExtensionFilter("HTML file","htm","html")
//chooser.addChoosableFileFilter(filter);
//Prior versions must use external extension of FileFilter class (ie ExtFilter)
//ExtFilter requires separate compile to enable choosable selection
String[] html = new String[] {"htm","html","xhtml"};
int option = chooser.showOpenDialog(browser.this);
if (option == JFileChooser.APPROVE_OPTION)
{
File[] sf = chooser.getSelectedFiles();
String filelist = "nothing";
try
{
if (sf.length > 0) filelist = sf[0].getCanonicalPath();
for (int i = 1; i < sf.length; i++)
{filelist += ", " + sf.getCanonicalPath();
tf.setText("our choices" + filelist);
}
}
catch (IOException evt) {System.out.println("Exception: "+evt);}
statusbar.setText("Your choices: " + filelist );
// String[] text = new String[100];
//getChars(0,100,text[],0);
l1.setText("your choices \n" + filelist);
}
else {statusbar.setText("You cancelled!");}
}
});
// add the open DIRECTORY chooser
dirButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
JFileChooser chooser = new JFileChooser(curDir);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setMultiSelectionEnabled(false);
int option = chooser.showOpenDialog(browser.this);
if (option == JFileChooser.APPROVE_OPTION)
{
File[] sf = chooser.getSelectedFiles();
String filelist = "nothing";
try
{
if (sf.length > 0) filelist = sf[0].getCanonicalPath();
for (int i = 1; i < sf.length; i++)
{filelist += ", " + sf.getCanonicalPath();}
}
catch (IOException evt) {System.out.println("Exception: "+evt);}
statusbar.setText("You chose " + filelist);
}
else {statusbar.setText("You cancelled!");}
}
});
System.out.println("Example of ZIP file creation.");
// Specify files to be zipped
String[] filesToZip = new String[1];
filesToZip[0] = "filelist";
byte[] buffer = new byte[18024];
// Specify zip file name
String zipFileName = "example.zip";
try {
ZipOutputStream out =
new ZipOutputStream(new FileOutputStream(zipFileName));
// Set the compression ratio
out.setLevel(Deflater.DEFAULT_COMPRESSION);
// iterate through the array of files, adding each to the zip file
for (int i = 0; i < filesToZip.length; i++) {
System.out.println(i);
// Associate a file input stream for the current file
FileInputStream in = new FileInputStream(filesToZip);
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(filesToZip));
// Transfer bytes from the current file to the ZIP file
//out.write(buffer, 0, in.read(buffer));
int len;
while ((len = in.read(buffer)) > 0)
{
out.write(buffer, 0, len);
}
// Close the current entry
out.closeEntry();
// Close the current file input stream
in.close();
}
// Close the ZipOutPutStream
out.close();
}
catch (IllegalArgumentException iae) {
iae.printStackTrace();
}
catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
c.add(openButton);
c.add(dirButton);
c.add(statusbar);
c.add(resetButton);
c.add(submitButton);
//c.add(tf);
c.add(l1);
Insets insets = c.getInsets();
Dimension size = openButton.getPreferredSize();
openButton.setBounds(745 + insets.left,115 + insets.top, size.width, size.height);
size = dirButton.getPreferredSize();
dirButton.setBounds(755 + insets.left,165 + insets.top, size.width, size.height);
size = statusbar.getPreferredSize();
statusbar.setBounds(755 + insets.left,215 + insets.top, size.width, size.height);
size = resetButton.getPreferredSize();
resetButton.setBounds(755 + insets.left,265 + insets.top, size.width, size.height);
size = submitButton.getPreferredSize();
submitButton.setBounds(755 + insets.left,465 + insets.top, size.width, size.height);
size = tf.getPreferredSize();
tf.setBounds(25 + insets.left,5 + insets.top, size.width + 150, size.height + 430);
size = l1.getPreferredSize();
l1.setBounds(25 + insets.left,5 + insets.top, size.width + 150, size.height + 430);
setSize(1000,800);
setVisible(true);
}
public static void main(String[] args)
{
new browser();
}
}

