How to add a jasper preview into a JPanel

大憨熊 提交于 2020-01-30 07:55:06

问题


I have created a small report using jasper reports and I previewed it using the jasper viewer as below,

con = JDBCConnectionPool.getInstance().checkOut();
            String fileName = getClass().getClassLoader().getResource("com/bio/ofm/mnu/views/reports/jasperReports/repAuditReport.jrxml").getFile();
            JasperReport report = JasperCompileManager.compileReport(fileName);
            JasperPrint print = JasperFillManager.fillReport(report, null, con);
            JasperViewer viewer = new JasperViewer(print);   
            viewer.setVisible(true);

But I need to show this report in a JPanel so, I tried as,

JasperViewer.setDefaultLookAndFeelDecorated(true);
            JRViewer jrv = new JRViewer(print);
            jrv.setPreferredSize(new Dimension(getSize()));
            JScrollPane reportScroll = new JScrollPane(jrv);
           panel1.add(reportScroll);

but the report is not showing as I expected, please explain what is the correct way to add a jasper preview in to a JPanel.


回答1:


JasperViewer extends JFrame, and you don't want to add a JFrame to a JPanel. You could perhaps add the JasperViewer's contentPane to the JPanel. The other issues I've discussed in a comment still remain unresolved:

What layout manager is panel1 using? Are you adding the JScrollPane to the JPanel after the GUI has been displayed? If so, do you call revalidate() and repaint() on the JPanel after the addition?




回答2:


this my code and it works

       JRViewer jr=new JRViewer(print); 
       jPanel1.setLayout(new BorderLayout());
       jPanel1.repaint();
       jPanel1.add(jr);
       jPanel1.revalidate();


来源:https://stackoverflow.com/questions/10645878/how-to-add-a-jasper-preview-into-a-jpanel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!