Swing应用程序桌面

时间:2012-06-01 08:53:04

标签: java swing

这是我的代码我有3个textfields和1个comboBox.I hava使用了mysql数据库和hibernate 我可以创建一个jasper报告(/home/webwerks/gitpro/Swing/src/report5.jrxml“)给出这样的路径它没关系但是我已经设置了动态路径到outfile(jasper报告)如何。你可以看到jButton5ActionPerformed代码我可以像这样给出outfile的静态路径(JasperExportManager.exportReportToPdfFile(jasperPrint,“/ home /webwerks / renport.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) {
            }
        }
    }                                       

1 个答案:

答案 0 :(得分:1)

您需要使用JFileChooser,如this tutorial

中所示
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.
}