-1

I am working on Jasper Report. I need to ask from a user where to save the generated report. For that, I need to open a "Save As" dialog box. I tried it using JFileChooser and FileDialog.

But, during execution of my code, when execution reaches the point where the code for the Save As dialog box is written, the code remains stuck there. One thing I noticed is that if you run the JFileChooser and FileDialog code for an open dialog box in a separate java class with its own PSVM, it works well.

But when it is invoked by some other function, execution remain stuck there.

Is there any plugin or jar I need to add to use JFileChooser and FileDialog? Or something else I am missing?

I am using eclipse Java EE kepler and Spring MVC.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
  • Please provide the relevant code, preferably as an MVCE ( http://stackoverflow.com/help/mcve ) – copeg Apr 13 '15 at 16:05

2 Answers2

0

A FileDialog is easily handled like this:

public String getFileFromDialog(){
  FileDialog dialog=new FileDialog(yourFrame,"Save as",FileDialog.SAVE);
  dialog.setVisible(true);
  //When you call setVisible(true), the method blocks until the user 
  //chooses a file or cancels the dialog
  return dialog.getDirectory()+dialog.getFile();
}

Be aware that, if the user closes the dialog without any file chosen the method will return the rather weird String "nullnull" as both getDirectory() and getFile() return the String "null" (not null but "null"). You probably have to check this to make sure the user has actually chosen a file.

0xJonas
  • 289
  • 1
  • 7
  • I have written the same code. But the dialog box did not open. Without loading the dialog box, program execution stuck at that point. – Anand Sushma Kadu Apr 14 '15 at 13:07
  • This sounds to me like there's an issue with your Frame class. Can you please post the code where you want to open the FileDialog? – 0xJonas Apr 14 '15 at 14:03
0
JFrame frame= new JFrame();
frame.setAlwaysOnTop(true);
frame.setVisible(true);
FileDialog fd = new FileDialog(frame, "Save as", FileDialog.SAVE);
fd.setAlwaysOnTop(true);
fd.setVisible(true);
String path=fd.getDirectory().toString()+fd.getFile().toString();
   input=cl.getResourceAsStream(total_employee_record);
report=JasperCompileManager.compileReport(input);
parameters.put("mypic",inputimage);
print=JasperFillManager.fillReport(report, parameters,new              JRBeanCollectionDataSource(beanlist,false));

JasperExportManager.exportReportToPdfFile(print,path); } .......... This is my service method. Now thing that happened is save as dialog box is opened,but in the background. Means everytime I have to minimize eclipse to set the path. Get me solution please. My apologies for the bad formating.