0

Hi Friends i want to Print the JTable content with Grid Could you please help me how to do that

I AM POSTING my CODE HERE

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.util.*;
import java.sql.*;
import java.awt.print.*;


public class Report extends JFrame implements ActionListener, Printable,Pageable
{
	private JTable table;
	private JToolBar toolbar;
	private JLabel lbl1;
	private JButton btnprint,btnexport,btnbook;
	private JPanel pnl1,chkpnl1,chkpnl2;
	private JTextArea txtarea;
	private JTableHeader header;
	private int printWidth;
	private int headerHeight;
	private int rowHeight;
	
	private PageFormat defaultPageFormat = new PageFormat();

	public Report() throws Exception
	{
		setSize(630,575);
		setTitle("Reports");
		btnprint = new JButton("Print");
		btnprint.addActionListener(this);
		btnexport = new JButton("Export");
		btnexport.addActionListener(this);
		btnbook = new JButton("Show");
		btnbook.addActionListener(this);
		toolbar = new JToolBar();
		String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"}; 
		Object[][] data = { {"Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false)},
	 	{"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)}, 
		{"Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false)}, 
		{"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)},
		 {"Philip", "Milne", "Pool", new Integer(10), new Boolean(false)},
		{"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)}, 
		{"Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false)}, 
		{"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)},
		 {"Philip", "Milne", "Pool", new Integer(10), new Boolean(false)}, 
			{"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)}, 
		{"Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false)}, 
		{"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)},
		 {"Philip", "Milne", "Pool", new Integer(10), new Boolean(false)},
		{"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)}, 
		{"Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false)}, 
		{"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)},
		 {"Philip", "Milne", "Pool", new Integer(10), new Boolean(false)}, 
			{"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)}, 
		{"Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false)}, 
		{"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)},
		 {"Philip", "Milne", "Pool", new Integer(10), new Boolean(false)},
		{"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)}, 
		{"Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false)}, 
		{"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)},
		 {"Philip", "Milne", "Pool", new Integer(10), new Boolean(false)},
		{"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)}, 
		{"Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false)}, 
		{"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)},
		 {"Philip", "Milne", "Pool", new Integer(10), new Boolean(false)},
		{"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)}, 
		{"Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false)}, 
		{"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)},
		 {"Philip", "Milne", "Pool", new Integer(10), new Boolean(false)},
		{"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)}, 
		{"Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false)}, 
		{"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)},
		 {"Philip", "Milne", "Pool", new Integer(10), new Boolean(false)},
		{"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)}, 
		{"Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false)}, 
		{"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)},
		 {"Philip", "Milne", "Pool", new Integer(10), new Boolean(false)},
		{"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)}, 
		{"Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false)}, 
		{"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)},
		 {"Philip", "Milne", "Pool", new Integer(10), new Boolean(false)},
		{"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)}, 
		{"Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false)}, 
		{"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)},
		 {"Philip", "Milne", "Pool", new Integer(10), new Boolean(false)},
		{"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)}, 
		{"Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false)}, 
		{"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)},
		 {"Philip", "Milne", "Pool", new Integer(10), new Boolean(false)},
		{"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)}, 
		{"Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false)}, 
		{"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)},
		 {"Philip", "Milne", "Pool", new Integer(10), new Boolean(false)}};
		 table = new JTable(data, columnNames);
		
		//table.setBorder(BorderFactory.createLineBorder(Color.black));
		
		header = table.getTableHeader();
		
		headerHeight = header.getPreferredSize().height;
		rowHeight = table.getRowHeight();

		//table.setEnabled(false);
		//table.setShowHorizontalLines(false);
		//table.setShowVerticalLines(false);
		
		Font fnt = new Font("serif",Font.BOLD,16);
		
		lbl1 = new JLabel("DON BOSCO SCHOOL LIBRARY");
		lbl1.setBorder(BorderFactory.createEmptyBorder(10,0,10,0));
		lbl1.setHorizontalAlignment(JLabel.CENTER);
		lbl1.setFont(fnt);
		pnl1 = new JPanel();
		chkpnl1 = new JPanel();
		chkpnl2 = new JPanel();
		//pnl1.setBorder(BorderFactory.createMatteBorder(8,8,8,8,Color.gray));
		//pnl1.setLayout(new BorderLayout());
		//pnl2 = new JPanel();
		//pnl2.setBackground(Color.white);
		//pnl2.add(table);

		//pnl1.add(lbl1,BorderLayout.NORTH);
		//pnl1.add(chkpnl1,BorderLayout.WEST);
		//pnl1.add(chkpnl2,BorderLayout.EAST);
		//pnl1.add(table,BorderLayout.CENTER);
		//pnl1.setBackground(Color.white);	
		  
		
		JScrollPane scrollpane = new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);//txtarea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS
		//scrollpane.setBackground(Color.red);
		
		
		toolbar.add(btnprint);	
		toolbar.addSeparator();
		toolbar.add(btnexport);
		toolbar.addSeparator();
	
		toolbar.add(btnbook);
		getContentPane().add(toolbar,BorderLayout.NORTH);
		getContentPane().add(scrollpane);
		
	}
	private int getNumberOfRowsByPage() {
		double tableAvailableSpaceByPage = defaultPageFormat.getImageableHeight()-headerHeight;
		return((int)Math.floor(tableAvailableSpaceByPage/rowHeight));
	}
 
	private int getTableVerticalShiftForIndex(int index) {
		int shiftY = (getNumberOfRowsByPage()*rowHeight*index);
		return(shiftY);
	}
 
	public int getNumberOfPages() {
		int nbPages = (int)Math.ceil(table.getRowCount()/(double)getNumberOfRowsByPage());
		return(nbPages);
	}
 
	public PageFormat getPageFormat(int pageIndex) throws IndexOutOfBoundsException {
		if (pageIndex<1 && pageIndex>getNumberOfPages()) {
			throw new IndexOutOfBoundsException("Page "+pageIndex+" doesn't exist!");
		}
		else {
			return(defaultPageFormat);
		}
	}
 
	public Printable getPrintable(int pageIndex) throws IndexOutOfBoundsException {
		if (pageIndex<1 && pageIndex>getNumberOfPages()) {
			throw new IndexOutOfBoundsException("Page "+pageIndex+" doesn't exist!");
		}
		else {
			return(this);
		}
	}

	public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
		printWidth = table.getWidth();
		System.out.println("THE PRINT WIDTH::"+printWidth);
		if (pageIndex<1 && pageIndex>getNumberOfPages()) {
			return Printable.NO_SUCH_PAGE;
		}	
		Graphics2D g = (Graphics2D)graphics;
		double scaleX = pageFormat.getImageableWidth()/printWidth;
		boolean needScale = scaleX < 1;
        g.translate(pageFormat.getImageableX(),pageFormat.getImageableY());
		if (needScale) {
			g.scale(scaleX,1);
		}
		g.setClip(0,0,printWidth,headerHeight);
		header.printAll(g);
		g.translate(0,headerHeight);
		g.setClip(0,0,printWidth, getNumberOfRowsByPage()*rowHeight);
		g.translate(0,-getTableVerticalShiftForIndex(pageIndex));
		table.printAll(g);
		return Printable.PAGE_EXISTS;
	}


	public void actionPerformed(ActionEvent ae)
	{
		Object obj = ae.getSource();
		if(obj==btnbook)
		{
			System.out.println("WORKING HERE");
			
		}
		if(obj==btnprint)
		{
			PrinterJob printerJob = PrinterJob.getPrinterJob();
			printerJob.setJobName("Printing table");
			defaultPageFormat = printerJob.defaultPage();
			defaultPageFormat = printerJob.pageDialog(defaultPageFormat);
			printerJob.setPageable(this);
			if (printerJob.printDialog()) 
			{			
				try {
					printerJob.print();
					System.out.println("Coming here TO print");
				}
				catch (PrinterException e)
				 {
					System.out.println(e);
				}	 
			}


		}
	}
	public static void main(String arg[])
	{
		try
		{
			Report rpt = new Report();
			rpt.setVisible(true);
			rpt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		}
		catch(Exception e)
		{
		}
	}
	
}

Please run my code and give me a Solution

Thank you
Jofin

2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by rdavid
0

Free Java table (JTable) based report API (Report Automatically from Java, J2EE and Swing Components).

Java report to generate, preview, print, and convert to PDF, Tiff, BMB, PNG, GIF, JPG/JPEG and more.

Download it for free at here:
http://www.activetree.com

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.