I have a button to print iReport but it take time to display the report so I made a loading frame class called Loading
and I am trying to call this class when I
Create New Undecorated frame with loading giffy or loading name.as LoadingScreen.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
LoadingScreen ls=new LoadingScreen();
ls.setVisible(true);// show frame before trying to connect to database and load report
try {
String reportquery = "Select * from invoices ";
JasperDesign jasperdesign = JRXmlLoader.load("StatementReport.jrxml");
JRDesignQuery designquery = new JRDesignQuery();
designquery.setText(reportquery);
jasperdesign.setQuery(designquery);
JasperReport jasperreport = JasperCompileManager.compileReport(jasperdesign);
JasperPrint jasperprint = JasperFillManager.fillReport(jasperreport, null, con);
ls.dispose();
JasperViewer.viewReport(jasperprint, false);
} catch (JRException e) {
JOptionPane.showMessageDialog(this, e);
} finally {
try {
rs.close();
pst.close();
} catch (SQLException ex) {
Logger.getLogger(showAllInvoices.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
I hope this will run
Like all questions relating to doing long running or blocking tasks in Swing, start with Concurrency in Swing to better understand the problem you're trying to solve. Next take a look at Worker Threads and SwingWorker for the most common solution
For example:
public static class JasperReportsWorker extends SwingWorker<JasperReport, JasperReport> {
@Override
protected JasperReport doInBackground() throws Exception {
JasperReport report = null;
try {
String reportquery = "Select * from invoices ";
JasperDesign jasperdesign = JRXmlLoader.load("StatementReport.jrxml");
JRDesignQuery designquery = new JRDesignQuery();
designquery.setText(reportquery);
jasperdesign.setQuery(designquery);
report = JasperCompileManager.compileReport(jasperdesign);
} finally {
try {
rs.close();
pst.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
return report;
}
}
Which could then be used something like:
JasperReportsWorker worker = new JasperReportsWorker();
JDialog dialog = new JDialog();
dialog.setModal(true);
dialog.add(new JLabel("Working..."));
dialog.pack();
dialog.setLocationRelativeTo(null);
worker.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
String name = evt.getPropertyName();
JasperReportsWorker worker = (JasperReportsWorker) evt.getSource();
if ("state".equalsIgnoreCase(name)) {
switch (worker.getState()) {
case DONE:
if (dialog != null) {
dialog.setVisible(false);
}
try {
JasperReport report = worker.get();
JasperPrint jasperprint = JasperFillManager.fillReport(jasperreport, null, con);
JasperViewer.viewReport(jasperprint, false);
} catch (InterruptedException | ExecutionException ex) {
ex.printStackTrace();
}
break;
}
}
}
});
worker.execute();
dialog.setVisible(true);
The are a number of variations around the basic idea, so feel free to experiment