• Post Reply Bookmark Topic Watch Topic
  • New Topic

cannot get a File[] array from jfilechooser

 
prabhat g
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am Trying to get a File[] array and pass it to a zipoutputstream to zip the selected files.I have also set the multiselection enabled for the filechooser.please help me out.
Also tell me about how to get multiple files selected from filedialog in AWT.I have the source code below for your reference.

public void actionPerformed(ActionEvent e)
{
if((e.getActionCommand())=="add file")
{
System.out.println("inside actionperformed line37");
JFileChooser chooser=new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setMultiSelectionEnabled(true);
chooser.setDialogTitle("choose files to zip");

int open=chooser.showOpenDialog(this);
System.out.println(" "+open);
if(open==JFileChooser.APPROVE_OPTION)
{
System.out.println(" "+open);
System.out.println("just above try ");
try {
System.out.println("inside try block line48");



File[] inputfiles=chooser.getSelectedFiles();
String[] inputfilenames=new String[inputfiles.length];
if(inputfiles.length >0)
System.out.println(inputfiles.length);
else
if(inputfiles.length ==0)
System.out.println(" 0 files");
else
System.out.println(" trouble" );
out1=new ZipOutputStream(new FileOutputStream("d:"+File.separator+"setup files"+File.separator+"zipfile.zip"));

for(int j=0;j<inputfiles.length;j++)<br /> {<br /> int len=0;<br /> System.out.println(" "+inputfiles.length);<br /> System.out.println(inputfiles[j].getPath());<br /> inputfilenames[j]=inputfiles[j].getPath();<br /> fis=new FileInputStream(inputfilenames[j]);<br /> System.out.println(fis);<br /> out1.putNextEntry(new ZipEntry(inputfilenames[j]));<br /> byte[] buffer=new byte[20000];<br /> while((len=fis.read(buffer))>0)
{
out1.write(buffer,0,len);
}
jta.setText(jta.getText()+"\n"+"deflating "+inputfiles[j].getName());
System.out.println(inputfiles[j].getName());
out1.closeEntry();

}
out1.close();
fis.close();
jta.setText(jta.getText()+"\n"+"deflating successfull"+"\n"+"zip file created");
}
catch(Exception ee) {ee.printStackTrace();}
}
}
}
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Prabhat,
According to my JDK1.2 API documentation the multiselection feature is not implemented by current Look & Feels (i.e, Windows, Mac, or Motif).
Manfred.
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prabhat,
Reregister with a user name that meets the requirements of Naming Policy. It requires that you have two names, seperated by a space and each at least 2 characters long. It can not be something cutesy or contain profanity in any language.

I ran your example with jdk 1.3 and was able to get a File[], you might consider upgrading.


------------------
Hope This Helps
Carl Trusiak
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!