English 中文(简体)
ireport 3.7.4 净户6.9.1
原标题:ireport 3.7.4 on netbeans 6.9.1 won t execute

I m new to java and jasperreport. 我很难执行一份小报告。 我增加了所有必要的杰尔档案,没有发现错误。

How can I produce a pdf file using netbeans auto generated "mouseclicked" event of a jbutton? Following is the code I ve been working from the tutorial of thainetbeans and ireport tutorial site:

import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.design.*;
import net.sf.jasperreports.view.*;
import net.sf.jasperreports.engine.xml.*;

public class MyiReportViewer extends javax.swing.JFrame {

    /** Creates new form MyiReportViewer */
    final String JDBC = "com.mysql.jdbc.Driver";
    final String DB = "jdbc:mysql://localhost/afemdb?user=root&password";
    private Connection con;
    /** Creates new form NewJPanel */
    public MyiReportViewer() {
        initComponents();
        try{
            Class.forName(JDBC).newInstance();
             con =DriverManager.getConnection(DB);
        }
        catch(Exception e){

        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("jButton1");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(95, 95, 95)
                .addComponent(jButton1)
                .addContainerGap(230, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(72, 72, 72)
                .addComponent(jButton1)
                .addContainerGap(205, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        


    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
        // TODO add your handling code here:
       try{
            //JasperDesign jasperDesign = JRXmlLoader.load("C:\Documents and Settings\Lelou\Desktop\presentation\InstructorQuestionaire.jrxml");
            //JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
            //JasperViewer.viewReport(jasperPrint, true);


              JasperReport report = JasperCompileManager.compileReport("C:\Documents and Settings\Lelou\Desktop\presentation\InstructorQuestionaire.jrxml");
              JasperPrint print = JasperFillManager.fillReport(report, null, con);
              JRViewer viewer = new JRViewer(print);
              viewer.setOpaque(true);
              viewer.setVisible(true);

            }
        catch(Exception e){

        }
    }                                     

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
            // try{
           // JasperDesign jasperDesign = JRXmlLoader.load("C:\Documents and Settings\Lelou\Desktop\presentation\InstructorQuestionaire.jrxml");
            // JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
           // JasperPrint  jasperPrint = JasperFillManager.fillReport(jasperReport, null, con);
           // JasperViewer.viewReport(jasperPrint, true);


          //JasperReport report = JasperCompileManager.compileReport("C:\Documents and Settings\Lelou\Desktop\presentation\InstructorQuestionaire.jrxml");
          //JasperPrint print = JasperFillManager.fillReport(report,null, con);
          //JRViewer viewer = new JRViewer(print);
          //viewer.setOpaque(true);
          //viewer.setVisible(true);

          //  }
          // catch(Exception e){
          //}
    }                                        

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MyiReportViewer().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    // End of variables declaration                   

}
最佳回答

在您的jButton1ActionPered方法中添加这一内容。 你不需要MouseClickedEvent。

try {
    URL reportFileURL = getClass().getResource("C:/Documents and Settings/Lelou/Desktop/presentation/InstructorQuestionaire.jrxml");
    File reportFile = new File(reportFileURL.toURI());
    JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
    JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, con);
    JasperViewer jv = new JasperViewer(jasperPrint);
    JDialog viewer = new JDialog(this, "Batch Report", true);
    viewer.setBounds(jv.getBounds());
    viewer.getContentPane().add(jv.getContentPane());
    viewer.setResizable(true);
    viewer.setIconImage(jv.getIconImage());
    viewer.setVisible(true);
} catch (JRException exc) {
   System.out.println(exc.getMessage());
} catch (URISyntaxException exs) {
   System.out.println(exs.getMessage());
} 

(*) 注 摆脱双重后lash!

进口:

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JDialog;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.view.JasperViewer;
import net.sf.jasperreports.engine.JasperPrint;

这里是你们需要的图书馆(即使用i Report 4.0.1):

enter image description here
Most of them are here: C:Program FilesJasperSoftiReport-4.0.1ireportmodulesext

检查你的联系。 无疑使用该建筑商:

public MyiReportViewer() {
    initComponents();
    try {
        Class.forName(JDBC);
        //con =DriverManager.getConnection(DB);
        try {
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/afemdb?useUnicode=true&characterEncoding=UTF-8",
                    "root", "password");
        } catch (SQLException e) {
            System.out.println("Con " + e);
        }

    } catch (ClassNotFoundException e) {
        System.out.println("SQL" + e);
    }
}

如果它仍然不工作,就必须对你的报告有点错误。

问题回答

暂无回答




相关问题
Netbeans CVS - existing repo - existing working copy

I m using Netbeans to develop with Drupal. I m trying to let Netbeans get drupal core and modules from the repository on drupal.org to my local working copy. Problem is: I already have a working ...

Java Library and Class Path Problems

Quick personal background: I was hired a few months ago as the sole .NET developer (C#) by a company whose other devs are all php devs. A week into the job I was told they wanted to switch to Java ...

Privileged operations in netbeans mobility

I m writing a Java ME app that will use privileged operations such as messaging. By default the user is prompted to confirm each of these operations, but I would like to run it as a background ...

Running unit tests on both windows and linux

Is there a way, a method, to be able to effectively run unit tests (phpunit) on both linux and windows? I need to do this because some parts of the system is only available under linux, but i do want ...

Dependency bundle (jar-files/sources/API docs) in Eclipse

I m developing various in-house extensions for JIRA, the issue tracker we use. So far I worked with Netbeans and everything worked like a charm. However, now I need to switch to Eclipse and I m ...

热门标签