如何使jasper报告JDialog的顶部

时间:2015-07-17 04:32:09

标签: jasper-reports jdialog jasper-plugin

我使用jbeper报告插件为Netbeans制作报告,我使用JDialog中的按钮来显示我的jasper报告。但报告显示我的JDialog,这里有什么问题?

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                                
    // TODO add your handling code here:
    Map<String, Object> params = new HashMap <>();
    try {
        Connection kon = DriverManager.getConnection(database,user,pass);
        JasperDesign jasperDesign = JRXmlLoader.load("Myreports.jrxml");
        JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, kon);
        JasperViewer jasperViewer = new JasperViewer(jasperPrint, false); 
        jasperViewer.setTitle("My Reports");
        jasperViewer.setVisible(true);
    } catch (SQLException | JRException e){
        e.printStackTrace();
    }
}

我是这样添加setModal:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                                
    // TODO add your handling code here:
    Map<String, Object> params = new HashMap <>();
    try {
        Connection kon = DriverManager.getConnection(database,user,pass);
        JasperDesign jasperDesign = JRXmlLoader.load("Myreports.jrxml");
        JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, kon);
        JasperViewer jasperViewer = new JasperViewer(jasperPrint, false); 
        jasperViewer.setTitle("My Reports");
        setModal(false);
        jasperViewer.setVisible(true);
        setModal(true);
    } catch (SQLException | JRException e){
        e.printStackTrace();
    }
}

我的报告显示了JDialog的顶部,但没有按钮。请解决方案!

3 个答案:

答案 0 :(得分:5)

您可以使用此代码将JR显示为JDialog:

//after creating jasperViewer//
jasperViewer = new JasperViewer(jasperPrint, false);
JDialog dialog = new JDialog(this);//the owner
dialog.setContentPane(jasperViewer.getContentPane());
dialog.setSize(jasperViewer.getSize());
dialog.setTitle("XXXXX");
dialog.setIconImage(Toolkit.getDefaultToolkit().getImage(
getClass().getResource("URL IMG")));
dialog.setVisible(true);

答案 1 :(得分:2)

基于Jasperviewer的内容创建对话框

import java.awt.BorderLayout;
import java.awt.Toolkit;

import javax.swing.JDialog;
import javax.swing.JPanel;

import net.sf.jasperreports.engine.DefaultJasperReportsContext;
import net.sf.jasperreports.engine.JasperPrint;

public class ReportViewer extends JDialog {

    private static final long serialVersionUID = 1L;

    protected net.sf.jasperreports.swing.JRViewer viewer;
    private JPanel pnlMain;

    public ReportViewer(JDialog parent, JasperPrint jasperPrint) {
        super(parent, Boolean.TRUE);

        initComponents();

        this.viewer = new net.sf.jasperreports.swing.JRViewer(DefaultJasperReportsContext.getInstance(), jasperPrint,
                null, null);

        this.pnlMain.add(this.viewer, BorderLayout.CENTER);

    }

    private void initComponents() {
        pnlMain = new javax.swing.JPanel();

        setTitle("JasperViewer");
        setIconImage(new javax.swing.ImageIcon(getClass().getResource("/net/sf/jasperreports/view/images/jricon.GIF"))
                .getImage());
        addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm();
            }
        });

        pnlMain.setLayout(new java.awt.BorderLayout());

        getContentPane().add(pnlMain, java.awt.BorderLayout.CENTER);

        pack();

        Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit();
        java.awt.Dimension screenSize = toolkit.getScreenSize();
        int screenResolution = toolkit.getScreenResolution();
        float zoom = ((float) screenResolution) / net.sf.jasperreports.swing.JRViewerPanel.REPORT_RESOLUTION;

        int height = (int) (550 * zoom);

        if (height > screenSize.getHeight()) {
            height = (int) screenSize.getHeight();
        }

        int width = (int) (750 * zoom);
        if (width > screenSize.getWidth()) {
            width = (int) screenSize.getWidth();
        }

        java.awt.Dimension dimension = new java.awt.Dimension(width, height);
        setSize(dimension);
        setLocation((screenSize.width - width) / 2, (screenSize.height - height) / 2);

    }

    private void exitForm() {

        this.setVisible(false);
        this.viewer.clear();
        this.viewer = null;
        this.getContentPane().removeAll();
        this.dispose();

    }

}

然后在模式框架上调用查看器自定义

new ReportViewer(this, jasperPrint).setVisible(Boolean.TRUE);

答案 2 :(得分:0)

而不是分别创建JDialog ,请使用:

JasperViewer的方法。
setAlwaysOnTop(true);
JasperViewer extends JFrame{}

JasperViewer jasperViewer = new JasperViewer(jasperPrint, false); 
  jasperViewer.setTitle("My Reports");
  setModal(false);
  jasperViewer.setAlwaysOnTop(true);  //here
  jasperViewer.setVisible(true);
  setModal(true);

即使每个打开的窗口,它也将始终位于JDialog的顶部。