显示 'Loading' 帧直到后台进程完成
Display 'Loading' frame until the background process finish
我有一个打印 iReport 的按钮,但显示报告需要时间,所以我制作了一个名为 Loading
的加载框架 class
当我按下按钮时我试图调用它 class 但它不能正常工作。
我的按钮代码 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 to better understand the problem you're trying to solve. Next take a look at 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);
基本思想有很多变体,所以请随意试验
使用加载 giffy 或加载 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);
}
}
}
我希望这会 运行
我有一个打印 iReport 的按钮,但显示报告需要时间,所以我制作了一个名为 Loading
的加载框架 class
当我按下按钮时我试图调用它 class 但它不能正常工作。
我的按钮代码 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 to better understand the problem you're trying to solve. Next take a look at 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);
基本思想有很多变体,所以请随意试验
使用加载 giffy 或加载 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);
}
}
}
我希望这会 运行