-2

这是我的代码,我有 3 个文本字段和 1 个组合框。我使用了 mysql 数据库和休眠我可以创建一个碧玉报告(/home/webwerks/gitpro/Swing/src/report5.jrxml")给这样的路径它没关系,但是我已经设置了outfile的动态路径(jasper report)。你可以看到jButton5ActionPerformed代码,我可以在这个地方给出这样的outfile的静态路径(JasperExportManager.exportReportToPdfFile(jasperPrint,“/home/webwerks/report.pdf”)我可以给出动态路径如何请帮助我

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

     if (evt.getSource() == jButton1) {
            String s = jTextField1.getText();
            String s1 = jTextField2.getText();
            String s2 = jTextField3.getText();
            String s4 = (String) jComboBox1.getSelectedItem();
            Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            Performance performance = new Performance();
            performance.setFirerating(s4);
            performance.setSoundrating(s);
            performance.setWidth(s1);
            performance.setMaxheight(s2);
            session.save(performance);
            session.flush();
        }
    }                                        

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        if (evt.getSource() == jButton5) {
            try {
                Session session = HibernateUtil.getSessionFactory().openSession();
                List l = ((org.hibernate.classic.Session) session).find("from Hibernate.Performance");
                Map parameters = new HashMap();
                InputStream input = new FileInputStream("/home/webwerks/gitpro/Swing/src/report5.jrxml");
                JasperDesign jasperDesign = JRXmlLoader.load(input);
                JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
                JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(l);
                JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, ds);
                JasperExportManager.exportReportToPdfFile(jasperPrint, "/home/webwerks/report.pdf");
            } catch (Exception e) {
            }
        }
    }                                       
4

1 回答 1

1

您需要使用 JFileChooser,如本教程所示

final JFileChooser fc = new JFileChooser();
// configure file chooser if needed
int returnVal = fc.showSaveDialog(jButton5);
if (returnVal == JFileChooser.APPROVE_OPTION) {
     String filename = fc.getSelectedFile().getAbsolutePath();
     // Put your code here to generate the report.
}
于 2012-06-01T09:19:45.180 回答