gpt4 book ai didi

java - 显示 'Loading'帧直到后台进程完成

转载 作者:行者123 更新时间:2023-12-02 04:26:28 25 4
gpt4 key购买 nike

我有一个打印 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);
}
}
} `

最佳答案

与所有与在 Swing 中执行长时间运行或阻塞任务相关的问题一样,以 Concurrency in Swing 开头更好地理解您要解决的问题。接下来看看Worker Threads and SwingWorker最常见的解决方案

例如:

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);

围绕基本思想有许多变体,因此请随意尝试

关于java - 显示 'Loading'帧直到后台进程完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56606577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com