Pagina 1 di 3 1 2 3 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 28

Discussione: jasper viewer

  1. #1
    Utente di HTML.it L'avatar di 1sirena
    Registrato dal
    Mar 2008
    Messaggi
    163

    jasper viewer

    ciao a tutti!!! sono riuscita a creare un file.jrxml con iReport e a creare da questo un file pdf ed excel, per visualizzarlo abbiamo pensato ad un bottone che faccia visualizzare nel jasper viewer il file pdf o excel:

    1)Jframe, che contiene il bottone:
    codice:
    package Schedule.DB;
    
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JPanel;
    
    import net.sf.jasperreports.engine.JasperFillManager;
    import net.sf.jasperreports.engine.JasperPrint;
    import net.sf.jasperreports.view.JasperViewer;
    
    
    public class JFrame extends javax.swing.JFrame {
    
    	private static final long serialVersionUID = 1L;
    	private JPanel jContentPane = null;
    	private JButton jButton = null;
    
    	public JFrame() {
    		super();
    		initialize();
    	}
    
    	private void initialize() {
    		this.setSize(300, 200);
    		this.setTitle("JFrame");
    		this.setContentPane(getJContentPane());
    	}
    
    	private JPanel getJContentPane() {
    		if (jContentPane == null) {
    			jContentPane = new JPanel();
    			jContentPane.setLayout(null);
    			jContentPane.add(getJButton(), null);
    		}
    		return jContentPane;
    	}
    
    	private JButton getJButton() {
    		if (jButton == null) {
    			jButton = new JButton();
    			jButton.setBounds(new Rectangle(115, 78, 80, 44));
    			jButton.addActionListener(new ActionListener() {
    				@SuppressWarnings("static-access")
    				public void actionPerformed(ActionEvent evt) {
    					System.out.println("jButton.actionPerformed, event="+evt);
    					SQLManager sqlm = new SQLManager("com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/schedule","root","root");
    					
    					try{
    						MainTest mt = new MainTest();
    						mt.main(null);
    						@SuppressWarnings("unused")
    						JasperPrint print=JasperFillManager.fillReport("C://TEMP/Report.jasper", null);
    						JasperViewer.viewReport(print,false);
    						
    					}catch(Exception e){
    					sqlm.close(); 
    					}
    jButton.actionPerformed
    				}
    			});
    		}
    		return jButton;
    	}
    
    }
    2)MainTest, che contiene il codice che genera i 2 file:
    codice:
    package Schedule.DB;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.HashMap;
    import java.util.Map;
    
    import net.sf.jasperreports.engine.JRExporterParameter;
    import net.sf.jasperreports.engine.JasperExportManager;
    import net.sf.jasperreports.engine.JasperFillManager;
    import net.sf.jasperreports.engine.JasperPrint;
    import net.sf.jasperreports.engine.export.JRXlsExporter;
    import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
    
    public class MainTest {
    
    MainTest() {
    }
    
    @SuppressWarnings({ "unchecked", "unchecked" })
    public static void main(String[] args) {
    try{
    
    //File jasper sorgente
    String fileName="C://TEMP/Report.jasper";
    //File pdf di destinazione
    String destFileNamePdf="C://TEMP/Report.pdf";
    //File xls di destinazione
    String destFileNameXls="C://TEMP/Report.xls";
    
    //Passaggio parametri da passare al jasper.
    Map parameters = new HashMap();
    parameters.put("param1", new Integer(1));
    
    //Preparazione del file da stampare (in questa fase si esegue la query e si inseriscono
    //i valori estratti dalla query)
    JasperPrint jasperPrint=JasperFillManager.fillReport(fileName, parameters, getConnection());
    
    //Creazione del PDF
    JasperExportManager.exportReportToPdfFile(jasperPrint, destFileNamePdf);
    
    //Creazione dell'xls
    JRXlsExporter exporter = new JRXlsExporter();
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFileNameXls);
    exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
    exporter.exportReport();
    
    System.exit(0);
    }catch (Exception e){
    e.printStackTrace();
    }
    
    }
    
    private static Connection getConnection() throws ClassNotFoundException, SQLException {
    String driver = "com.mysql.jdbc.Driver";
    String connectString = "jdbc:mysql://localhost:3306/schedule";
    String user = "root";
    String password = "root";
    
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(connectString, user, password);
    return conn;
    }
    
    }
    alla fine di tutto ciò i file vengono creati, ma non riesco a visualizzarli, visto che secondo l'errore che da' il jasper viewer risulta vuoto ...

    [errore]
    jButton.actionPerformed, event=java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=,when=1215187299636,modifiers =Button1] on javax.swing.JButton[,115,78,80x44,alignmentX=0.0,alignmentY=0.5,border =javax.swing.plaf.BorderUIResource$CompoundBorderU IResource@32fb4f,flags=296,maximumSize=,minimumSiz e=,preferredSize=,defaultIcon=,disabledIcon=,disab ledSelectedIcon=,margin=javax.swing.plaf.InsetsUIR esource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rol loverEnabled=true,rolloverIcon=,rolloverSelectedIc on=,selectedIcon=,text=,defaultCapable=true]
    103502 [AWT-EventQueue-0] WARN query.JRJdbcQueryExecuter - The supplied java.sql.Connection object is null.

    [/errore]

  2. #2
    Utente di HTML.it
    Registrato dal
    Mar 2007
    Messaggi
    43
    Secondo me non puoi chiamare un main se sei dentro ad un oggetto.
    Dovresti modificare la struttura
    by yakino @doc

  3. #3
    Utente di HTML.it L'avatar di 1sirena
    Registrato dal
    Mar 2008
    Messaggi
    163
    codice:
    package Schedule.DB;
    
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JPanel;
    
    import net.sf.jasperreports.engine.JasperFillManager;
    import net.sf.jasperreports.engine.JasperPrint;
    import net.sf.jasperreports.view.JasperViewer;
    
    /**
    * This code was edited or generated using CloudGarden's Jigloo
    * SWT/Swing GUI Builder, which is free for non-commercial
    * use. If Jigloo is being used commercially (ie, by a corporation,
    * company or business for any purpose whatever) then you
    * should purchase a license for each developer using Jigloo.
    * Please visit www.cloudgarden.com for details.
    * Use of Jigloo implies acceptance of these licensing terms.
    * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
    * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
    * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
    */
    public class JFrame extends javax.swing.JFrame {
    
    	private static final long serialVersionUID = 1L;
    	private JPanel jContentPane = null;
    	private JButton jButton = null;
    
    	/**
    	 * This is the default constructor
    	 */
    	public JFrame() {
    		super();
    		initialize();
    	}
    
    	/**
    	 * This method initializes this
    	 * 
    	 * @return void
    	 */
    	private void initialize() {
    		this.setSize(300, 200);
    		this.setTitle("JFrame");
    		this.setContentPane(getJContentPane());
    	}
    
    	/**
    	 * This method initializes jContentPane
    	 * 
    	 * @return javax.swing.JPanel
    	 */
    	private JPanel getJContentPane() {
    		if (jContentPane == null) {
    			jContentPane = new JPanel();
    			jContentPane.setLayout(null);
    			jContentPane.add(getJButton(), null);
    		}
    		return jContentPane;
    	}
    
    	/**
    	 * This method initializes jButton	
    	 * 	
    	 * @return javax.swing.JButton	
    	 */
    	private JButton getJButton() {
    		if (jButton == null) {
    			jButton = new JButton();
    			jButton.setBounds(new Rectangle(115, 78, 80, 44));
    			jButton.addActionListener(new ActionListener() {
    				public void actionPerformed(ActionEvent evt) {
    					System.out.println("jButton.actionPerformed, event="+evt);
    					SQLManager sqlm = new SQLManager("com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/schedule","root","root");
    					
    					try{
    						//MainTest mt = new MainTest();
    						//mt.main(null);
    						@SuppressWarnings("unused")
    						JasperPrint print=JasperFillManager.fillReport("C://TEMP/Report.jasper", null);
    						JasperViewer.viewReport(print,false);
    						
    					}catch(Exception e){
    					sqlm.close(); 
    					}
    					//TODO add your code for jButton.actionPerformed
    				}
    			});
    		}
    		return jButton;
    	}
    
    }
    purtoppo anke nn chiamando il main dell'altra classe MainTest mi continua a dire ke il documento non ha pagine:

    [errore]
    jButton.actionPerformed, event=java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=,when=1215194878172,modifiers =Button1] on javax.swing.JButton[,115,78,80x44,alignmentX=0.0,alignmentY=0.5,border =javax.swing.plaf.BorderUIResource$CompoundBorderU IResource@32fb4f,flags=296,maximumSize=,minimumSiz e=,preferredSize=,defaultIcon=,disabledIcon=,disab ledSelectedIcon=,margin=javax.swing.plaf.InsetsUIR esource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rol loverEnabled=true,rolloverIcon=,rolloverSelectedIc on=,selectedIcon=,text=,defaultCapable=true]
    0 [AWT-EventQueue-0] WARN query.JRJdbcQueryExecuter - The supplied java.sql.Connection object is null.
    [/errore]

  4. #4
    Utente di HTML.it
    Registrato dal
    Mar 2007
    Messaggi
    43
    Io ho fatto così in una mia applicazione
    Nella grafica stampa è un bottone
    Codice PHP:

    stampa
    .addActionListener(new ActionListener() {             
    public 
    void actionPerformed(ActionEvent arg0) {                 
                    
    new 
    Reporter(conn).start();     
                    
    }         
    }); 
    Ecco la classe report

    Codice PHP:
    public class Reporter extends Thread {
        private 
    JasperPrint jp;
        private 
    String REPORTS_FOLDER "C:\\";//File .jrxml
        
    private Connection conn;
        public 
    Reporter(Connection conn){
            
    this.conn=conn;
        }
        public 
    void run() {
            
    String reportName="Prova";
            
    String reportPath REPORTS_FOLDER reportName;
            
    JasperReport jr null;
            try {
                
    jr=ReportLoader.loadReport(reportPath);
                
    jp ReportFiller.fillReport(connnulljr);
                new 
    JasperViewer(jp,false).setVisible(true);
            } catch (
    JRException jre) {
                
    JOptionPane.showMessageDialog(null,"Errore nella creazione del report");
            }
        }

    E il gioco è fatto.
    Fammi Sapere
    by yakino @doc

  5. #5
    Utente di HTML.it L'avatar di 1sirena
    Registrato dal
    Mar 2008
    Messaggi
    163
    codice:
    package Schedule.DB;
    
    import javax.swing.JOptionPane;
    
    import com.mysql.jdbc.Connection;
    
    import net.sf.jasperreports.engine.JRException;
    import net.sf.jasperreports.engine.JasperPrint;
    import net.sf.jasperreports.engine.JasperReport;
    import net.sf.jasperreports.view.JasperViewer;
    import nickyb.sqleonardo.environment.ctrl.comparer.ReportPane;
    
    public class Report extends Thread { 
        private JasperPrint jp; 
        private String REPORTS_FOLDER = "C://TEMP/Report.jrxml"; 
        private Connection conn; 
        public Report(Connection conn){ 
            this.conn=conn; 
        } 
        public void run() { 
            String reportName="Prova"; 
            String reportPath = REPORTS_FOLDER + reportName; 
            JasperReport jr = null; 
            try { 
                jr=ReportLoader.loadReport(reportPath); 
                jp = ReportFiller.fillReport(conn, null, jr); 
                new JasperViewer(jp,false).setVisible(true); 
            } catch (JRException jre) { 
                JOptionPane.showMessageDialog(null,"Errore nella creazione del report"); 
            } 
        } 
    }
    ho provato, ma facendo cm dici tu non mi riconosce il ReportLoader ed il ReportFiller --- pekke'????

  6. #6
    Utente di HTML.it
    Registrato dal
    Mar 2007
    Messaggi
    43
    Hai importato le librerie di Ireport?
    by yakino @doc

  7. #7
    Impariamo a leggere i messaggi nelle eccezioni


    jButton.actionPerformed, event=java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=,when=1215194878172,modifiers =Button1] on javax.swing.JButton (...) WARN query.JRJdbcQueryExecuter - The supplied java.sql.Connection object is null.

    Il problema sta nel fatto che al metodo JasperFillManager.fillReport(...) viene passata una connessione al DB che è null.

    Al mio segnale... scatenate l'inferno!

  8. #8
    Utente di HTML.it L'avatar di 1sirena
    Registrato dal
    Mar 2008
    Messaggi
    163
    si r@ve me ne sn accorta ... uffa!!! non so' cm risolvere!?
    ho provato a modificare qualcosa:

    1)JFrame con Bottone:
    codice:
    package Schedule.DB;
    
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JPanel;
    
    
    /**
    * This code was edited or generated using CloudGarden's Jigloo
    * SWT/Swing GUI Builder, which is free for non-commercial
    * use. If Jigloo is being used commercially (ie, by a corporation,
    * company or business for any purpose whatever) then you
    * should purchase a license for each developer using Jigloo.
    * Please visit www.cloudgarden.com for details.
    * Use of Jigloo implies acceptance of these licensing terms.
    * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
    * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
    * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
    */
    public class ProvaJframe extends javax.swing.JFrame {
    
    	private static final long serialVersionUID = 1L;
    	private JPanel jContentPane = null;
    	private JButton jButton = null;
    
    	/**
    	 * This is the default constructor
    	 */
    	public ProvaJframe() {
    		super();
    		initialize();
    	}
    
    	/**
    	 * This method initializes this
    	 * 
    	 * @return void
    	 */
    	private void initialize() {
    		this.setSize(300, 200);
    		this.setTitle("JFrame");
    		this.setContentPane(getJContentPane());
    	}
    
    	/**
    	 * This method initializes jContentPane
    	 * 
    	 * @return javax.swing.JPanel
    	 */
    	private JPanel getJContentPane() {
    		if (jContentPane == null) {
    			jContentPane = new JPanel();
    			jContentPane.setLayout(null);
    			jContentPane.add(getJButton(), null);
    		}
    		return jContentPane;
    	}
    
    	/**
    	 * This method initializes jButton	
    	 * 	
    	 * @return javax.swing.JButton	
    	 */
    	private JButton getJButton() {
    		if (jButton == null) {
    			jButton = new JButton();
    			jButton.setBounds(new Rectangle(115, 78, 80, 44));
    			jButton.addActionListener(new ActionListener() {              
    				public void actionPerformed(ActionEvent arg0) {                  
    					SQLManager sqlm = new SQLManager("com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/schedule","root","root");
    					
    					try{
    						                 
    				new Report(null).start();      
    					}catch(Exception e){
    						sqlm.close(); 
    					}}}
    				);
    }
    		
    		return jButton;
    	}
    
    }
    2)TestReport
    codice:
    package Schedule.DB;
    
    import javax.swing.JOptionPane;
    
    import net.sf.jasperreports.engine.JRException;
    import net.sf.jasperreports.engine.JasperFillManager;
    import net.sf.jasperreports.engine.JasperPrint;
    import net.sf.jasperreports.engine.design.JasperDesign;
    import net.sf.jasperreports.engine.xml.JRXmlLoader;
    import net.sf.jasperreports.view.JasperViewer;
    
    import com.mysql.jdbc.Connection;
    public class Report extends Thread { 
        private JasperPrint jp; 
        private String REPORTS_FOLDER = "C://TEMP/Report.jrxml"; 
        private Connection conn; 
        JasperDesign jr = null; 
        
        public Report(Connection conn){ 
            this.conn=conn; 
        } 
        public void run() { 
            String reportName="Prova"; 
            String reportPath = REPORTS_FOLDER + reportName; 
            try { 
    
            	jr = JRXmlLoader.load( reportPath+ ".jrxml");
            	jp = JasperFillManager.fillReport(reportPath + ".jasper", null, conn);
            		
            	      
            	 new JasperViewer(jp,false).setVisible(true); 
            } catch (JRException jre) { 
                JOptionPane.showMessageDialog(null,"Errore nella creazione del report"); 
            } 
        } 
    }
    e al click passa direttamente al messaggio "Errore nella creazione del report" ...


    AIUTATEMI!!!!

  9. #9
    Utente di HTML.it
    Registrato dal
    Mar 2007
    Messaggi
    43
    Prova a togliere
    Codice PHP:
    public void run() { 
           
            
    String reportPath REPORTS_FOLDER
            try { 

                
    jr JRXmlLoader.load(reportPath);
                
    jp JasperFillManager.fillReport(reportPath ".jasper"nullconn);
                    
                      
                 new 
    JasperViewer(jp,false).setVisible(true); 
            } catch (
    JRException jre) { 
                
    JOptionPane.showMessageDialog(null,"Errore nella creazione del report"); 
            } 
        } 
    Fammi Sapere
    by yakino @doc

  10. #10
    Utente di HTML.it L'avatar di 1sirena
    Registrato dal
    Mar 2008
    Messaggi
    163
    ho provato ... ma nn fa assolutamente nulla ... al click nn fa nulla!!!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.