I am creating a java desktop application. In the application I generate report using dynamic report and based on some parameter the report will generate. The user generates the first report and it is then displayed in a jasper viewer. The user minimizes the jasper viewer and generates next report. It is then viewed in a new jasper viewer.
So each time the user generates a report a new jasper viewer is created. How do I reuse or ensure only one JasperViewer exists at a time?
JasperPrint print=report.toJasperPrint();
JasperViewer.viewReport(print, false);
After that I created object for jasperviewer.
JasperViewer jv;
JasperPrint print;
public void action()
{
button.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
try
{
report.addColumn(...);
...
...
print=report.toJasperPrint();
jv=new JasperViewer(print,false);
jv.setVisible(true);
}
});
}
}
Just to start off, you sample code does not compile. Once formatted correctly, you see that the closing parenthesis and semicolon happen to soon. It should look like:
Now to the question.
JasperViewerextendsJFrameand as such any tricks to close it programatically should also work forJasperViewer. Since you are storing a reference to theJasperViewer, you could check if it is already initialized. If it is send a window event to it to close it.