I am developing Java desktop based application using Swing framework and JasperReports for reporting. I have some problems, when JasperVie
For setting the default extension(s) to save to, one idea is that you need to somehow get the JRViewer
instance from a JasperViewer
(instance) and then, on the void JRViewer
instance, you can set some save contributions. Have a look at JRViewer.setSaveContributors(JRSaveContributor[] saveContribs).
For setting the file name, i am not entirely sure, but have a look at JRViewer.JRViewer(String, boolean).
Also check the JRViewer and the JasperViewer source code, it may come in handy.
It is an old post but I ran into the same problem...
I extended my viewer component from net.sf.jasperreports.view.JRViewer. After some code review I saw that it is only possible to control the 'lastFolder' but not the filename from outside. So you can set the last used folder if it is not set already by:
if (lastFolder == null) {
this.lastFolder = new File(System.getProperty("user.home"));
}
But, and thats the hint: All buttons of toolbar are public fields!
So for my solution I removed all ActionListeners of the save button (btnSave):
for (ActionListener actionListener : this.btnSave.getActionListeners()) {
this.btnSave.removeActionListener(actionListener);
}
And then added my own implementation. It is pretty like the original one only with the difference that I control the file name depending to the report name and current timestamp.
...
File file = new File(lastFolder.getPath() + System.getProperty("file.separator") + DateTime.now().toString() + "_" + jasperPrint.getName());
...
Additionally I set the pre-selected file extension from .jasperprint to .pdf which is more convenient.
...
fileChooser.setFileFilter((FileFilter) saveContributors.get(1));
...
The full ActionListener code is as follows:
this.btnSave.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setLocale(getLocale());
fileChooser.updateUI();
File file = new File(lastFolder.getPath() + System.getProperty("file.separator") + DateTime.now().toString() + "_" + jasperPrint.getName());
fileChooser.setSelectedFile(file);
for (int i = 0; i < saveContributors.size(); i++)
fileChooser.addChoosableFileFilter((FileFilter) saveContributors.get(i));
if (saveContributors.contains(lastSaveContributor))
fileChooser.setFileFilter(lastSaveContributor);
else if (saveContributors.size() > 1)
fileChooser.setFileFilter((FileFilter) saveContributors.get(1));
if (lastFolder != null)
fileChooser.setCurrentDirectory(lastFolder);
int retValue = fileChooser.showSaveDialog(JasperViewer.this);
if (retValue == 0) {
FileFilter fileFilter = fileChooser.getFileFilter();
file = fileChooser.getSelectedFile();
lastFolder = file.getParentFile();
JRSaveContributor contributor = null;
if (fileFilter instanceof JRSaveContributor) {
contributor = (JRSaveContributor) fileFilter;
} else {
int i = 0;
do {
if (contributor != null || i >= saveContributors.size())
break;
contributor = (JRSaveContributor) saveContributors.get(i++);
if (!contributor.accept(file))
contributor = null;
} while (true);
if (contributor == null)
contributor = new JRPrintSaveContributor(jasperReportsContext, getLocale(), null);
}
lastSaveContributor = contributor;
try {
contributor.save(jasperPrint, file);
} catch (JRException ex) {
logger.error("Could not save report.", ex);
JOptionPane.showMessageDialog(JasperViewer.this, trc("JasperViewer.error.save", "Could not save report."));
}
}
}
});