Printing Report in Core Java

It was before one week, when I was searching and trying all possibilities to get report in Core Java, for my college project. I was thinking “Why Java, for each control you are the best but why not for printing report”.  But after long search I found a post by Sergey Groznyh in Java Webblog, where he had given the idea and example to generate print preview of swing Text Component. This really works for me after some Changes.I heartily  thanks Sergey Groznyh for his post. His post is in http://weblogs.java.net/blog/2007/09/05/generating-print-preview-swing-text-components.

In this post you will be able to preview a HTML page, where as in my project I had to preview the JTable content. For solution I add the JTable contents in a two dimensional array and using loop I add the content into a string and set that string to the editor pan, which really works.

Here us the code with changes:

/**
 *
 * @author prashant
 */
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.net.URL;

public class Form_JobRPT extends JInternalFrame
{
    JPanel panel_Content = new JPanel();
    static JButton btn_CompJobPrev;
    static JEditorPane ep = new JEditorPane();

    //MDIDesktopManager desktop;
    public Form_JobRPT()
    {

        setSize(1000,600);
        setTitle("Report Preview");
        setMaximizable(true);
        setIconifiable(true);
        setClosable(true);
        setResizable(true);

        final PrintPreview pp = new PrintPreview();

        ep.setEditable(false);
        ep.setContentType("text/html");

        String str="";
        str+="<html><head></head><body><table border=0 color='gray' cellpadding=5 cellspacing=0>";

        str+="<tr><th><font color='#0000FF'><u>Code</u></font></th>" +
                "<th><font color='#0000FF'><u>Company Name</u></font></th>" +
                "<th><font color='#0000FF'><u>Designation</u></font></th>" +
                "<th><font color='#0000FF'><u>Opening Date</u></font></th>" +
                "<th><font color='#0000FF'><u>Closing Date</u></font></th>" +
                "<th><font color='#0000FF'><u>Ann. Salary</u></font></th></tr>";
        for(int i=0;i<Form_JobTbl.dataRow;i++)
         {
            str+="<tr>";
            for(int j=0;j<Form_JobTbl.dataCol;j++)
            {
                str+="<td>";
                str+=Form_JobTbl.mydata[i][j]+"<b>";
                str+="</td>";
            }
            str+="</tr>";
         }
        str+="</table></body>";

        ep.setText(str);
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(panel_Content);
        panel_Content.setLayout(null);
        panel_Content.setBounds(0, 0, getBounds().width, getBounds().height);
        btn_CompJobPrev=new JButton(new ImageIcon("icons/print_preview.png"));
        btn_CompJobPrev.setToolTipText("Print Preview");
        btn_CompJobPrev.setBounds(10, 30, 40, 40);
        panel_Content.add(btn_CompJobPrev);
        btn_CompJobPrev.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                pp.showPreview(ep);
            }
        });
        int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
        int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
        JScrollPane jsp = new JScrollPane(ep,v,h);
        jsp.setBounds(10, 100, getBounds().width-5, getBounds().height-150);
        panel_Content.add(jsp);
    }
}

/**

*

* @author prashant

*/

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

import java.net.URL;

public class Form_JobRPT extends JInternalFrame

{

JPanel panel_Content = new JPanel();

static JButton btn_CompJobPrev;

static JEditorPane ep = new JEditorPane();

//MDIDesktopManager desktop;

public Form_JobRPT()

{

setSize(1000,600);

setTitle(“Report Preview”);

setMaximizable(true);

setIconifiable(true);

setClosable(true);

setResizable(true);

final PrintPreview pp = new PrintPreview();

ep.setEditable(false);

ep.setContentType(“text/html”);

String str=””;

str+=”<html><head></head><body><table border=0 color=’gray’ cellpadding=5 cellspacing=0>”;

str+=”<tr><th><font color=’#0000FF’><u>Code</u></font></th>” +

“<th><font color=’#0000FF’><u>Company Name</u></font></th>” +

“<th><font color=’#0000FF’><u>Designation</u></font></th>” +

“<th><font color=’#0000FF’><u>Opening Date</u></font></th>” +

“<th><font color=’#0000FF’><u>Closing Date</u></font></th>” +

“<th><font color=’#0000FF’><u>Ann. Salary</u></font></th></tr>”;

for(int i=0;i<Form_JobTbl.dataRow;i++)

{

str+=”<tr>”;

for(int j=0;j<Form_JobTbl.dataCol;j++)

{

str+=”<td>”;

str+=Form_JobTbl.mydata[i][j]+”<b>”;

str+=”</td>”;

}

str+=”</tr>”;

}

str+=”</table></body>”;

ep.setText(str);

getContentPane().setLayout(new BorderLayout());

getContentPane().add(panel_Content);

panel_Content.setLayout(null);

panel_Content.setBounds(0, 0, getBounds().width, getBounds().height);

btn_CompJobPrev=new JButton(new ImageIcon(“icons/print_preview.png”));

btn_CompJobPrev.setToolTipText(“Print Preview”);

btn_CompJobPrev.setBounds(10, 30, 40, 40);

panel_Content.add(btn_CompJobPrev);

btn_CompJobPrev.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

pp.showPreview(ep);

}

});

int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;

int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;

JScrollPane jsp = new JScrollPane(ep,v,h);

jsp.setBounds(10, 100, getBounds().width-5, getBounds().height-150);

panel_Content.add(jsp);

}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: