2
votes

How can I view my jrxml file inside a JFrame?

I have the following java code:

package test;

import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JFrame;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.swing.JRViewer;


public class Test extends JFrame {

    private Connection conn = null; // database connection

    public Test() {

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = DriverManager.getConnection(
                    "jdbc:oracle:thin:@localhost:1521:XE",
                    "test", "test");
            JasperReport report = JasperCompileManager.compileReport("report1.jrxml");
            JasperPrint print = JasperFillManager.fillReport(report, null, conn);
            JRViewer viewer = new JRViewer(print);
            viewer.setOpaque(true);
            viewer.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
            } catch (Exception e) {
            }
        }

        setSize(500, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new Test();
    }
}

However, when I try running it, I received the following error message:

log4j:WARN No appenders could be found for logger(net.sf.jasperreports.engine.xml.JRXmlDigesterFactory).

log4j:WARN Please initialize the log4j system properly.

My JFrame appears but but without the report I want to embed.

What do you think is my error?

1

1 Answers

3
votes

JRViewer is a JPanel, so instead of..

viewer.setVisible(true);

Put ..

// Add the viewer to the frame
add(viewer);

As an aside:

} catch (Exception e) {
}

Change that, to..

} catch (Exception e) {
    e.printStackTrace();
}