我有一个打印iReport的按钮,但是显示报告需要时间,所以我创建了一个名为Loading
的加载框架类
当我按下按钮时,我试图打电话给这个班级,但是它没有正常工作。
按钮ActionListener
的代码是:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
loading.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);
JasperViewer.viewReport(jasperprint, false);
} catch (JRException e) {
JOptionPane.showMessageDialog(this, e);
} finally {
try {
rs.close();
pst.close();
loading.dispose(); // dispose loading frame when finish
} catch (SQLException ex) {
Logger.getLogger(showAllInvoices.class.getName()).log(Level.SEVERE, null, ex);
}
}
} `
答案 0 :(得分:3)
与所有与在Swing中执行长时间运行或阻止任务相关的问题一样,请从A technique for saving content from a data:text/html, URI开始,以便更好地了解您尝试解决的问题。接下来看一下Concurrency in Swing最常见的解决方案
例如:
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;
}
}
然后可以使用以下内容:
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);
围绕基本理念有很多变化,所以请随意尝试
答案 1 :(得分:0)
创建带有加载吉菲或加载名称的新未装饰框架。作为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);
}
}
}
我希望这会运行