0
我有一个按钮来打印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);
}
}
} `
1)不要阻塞EDT(Event Dispatch Thread)。发生这种情况时,GUI将“冻结”。有关详细信息和修补程序,请参见[Swing中的并发](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/)。 2)请参阅[使用多个JFrames,好/坏实践?](http://stackoverflow.com/q/9554636/418556)似乎这将更适合'JDialog'。 –
我添加'jDialog',但它出现在报告 'jDialog.setLayout(new GridBagLayout()); jDialog.add(new JLabel(“Please wait ...”)); jDialog.setMinimumSize(new Dimension(150,50)); jDialog.setResizable(false); jDialog.setModal(false); jDialog.setUndecorated(true); jDialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); jDialog.setLocationRelativeTo(null); jDialog.setVisible(true);'@AndrewThompson –
1)不要把代码放在注释中,它几乎不可读。 2)重要的建议是在* JDialog之前。在这种情况下,使用对话框不会修复损坏的东西(阻止EDT)。 –