Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Print JTable with the help of java

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 25-11-2009
Member
 
Join Date: Nov 2008
Posts: 85
Print JTable with the help of java
  

Hi, I want to print Jtable in java, But it is not printing. The code which I have downloaded from internet is not working properly. Please suggest me any solution. Please reply me as soon as possible. I am waiting for your reply.

Reply With Quote
  #2  
Old 25-11-2009
Member
 
Join Date: May 2008
Posts: 2,283
Re: Print JTable with the help of java

Hi, you can use following code to do this. When I was making my project I had got same problem. But then I solve that problem with the help of code by downloading from internet. Just try to use it. It will solve your problem.

Code:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PrinterException;
import java.text.MessageFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class MyClass 
{
public static void main(String args[]) 
{
String rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
String headers[] = { "Upper", "Lower" };

JFrame frame = new JFrame("Table to be Printed");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTable tble = new JTable(rows, headers);
JScrollPane scrollPane = new JScrollPane(tble);
frame.add(scrollPane, BorderLayout.CENTER);
JButton btn = new JButton("Print JTable");
ActionListener printAction = new ActionListener() 
{
public void actionPerformed(ActionEvent e) 
{
try 
{
MessageFormat headerFormat = new MessageFormat("Page {0}");
MessageFormat footerFormat = new MessageFormat("- {0} -");
table.print(JTable.PrintMode.FIT_WIDTH, headerFormat, footerFormat);
} 
catch (PrinterException e) 
{
System.err.println("Error while printing: " + e.getMessage());
}
}
}
button.addActionListener(printAction);
frame.add(btn, BorderLayout.SOUTH);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
Reply With Quote
  #3  
Old 25-11-2009
Member
 
Join Date: Oct 2005
Posts: 2,385
Re: Print JTable with the help of java

Hi, I am also new in java programming. But, some days ago in my classes I had given a code of printing JTable with the help of java. So, I am sending that code to you, You can just make use of it. Do, changes in your code and use it. I think this will be helpful for you.

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import java.text.*;

public class TablePrint 
{
public static void main(String args[]) 
{
final Object rows[][] = 
{
      {"one",   "1"},
      {"two",   "2"},
      {"three", "3"},
      {"four",  "4"},
      {"five",  "5"},
      {"six",   "6"},
      {"seven", "7"},
      {"eight", "8"},
      {"nine",  "9"},
      {"ten",   "10"},
      {"one",   "1"},
      {"two",   "2"},
      {"three", "3"},
      {"four",  "4"},
      {"five",  "5"},
      {"six",   "6"},
      {"seven", "7"},
      {"eight", "8"},
      {"nine",  "9"},
      {"ten",   "10"},
      {"one",   "1"},
      {"two",   "2"},
      {"three", "3"},
      {"four",  "4"},
      {"five",  "5"},
      {"six",   "6"},
      {"seven", "7"},
      {"eight", "8"},
      {"nine",  "9"},
      {"ten",   "10"},
      {"one",   "1"},
      {"two",   "2"},
      {"three", "3"},
      {"four",  "4"},
      {"five",  "5"},
      {"six",   "6"},
      {"seven", "7"},
      {"eight", "8"},
      {"nine",  "9"},
      {"ten",   "10"},
      {"one",   "1"},
      {"two",   "2"},
      {"three", "3"},
      {"four",  "4"},
      {"five",  "5"},
      {"six",   "6"},
      {"seven", "7"},
      {"eight", "8"},
      {"nine",  "9"},
      {"ten",   "10"},
      {"one",   "1"},
      {"two",   "2"},
      {"three", "3"},
      {"four",  "4"},
      {"five",  "5"},
      {"six",   "6"},
      {"seven", "7"},
      {"eight", "8"},
      {"nine",  "9"},
      {"ten",   "10"},
      {"one",   "1"},
      {"two",   "2"},
      {"three", "3"},
      {"four",  "4"},
      {"five",  "5"},
      {"six",   "6"},
      {"seven", "7"},
      {"eight", "8"},
      {"nine",  "9"},
      {"ten",   "10"},
      {"one",   "1"},
      {"two",   "2"},
      {"three", "3"},
      {"four",  "4"},
      {"five",  "5"},
      {"six",   "6"},
      {"seven", "7"},
      {"eight", "8"},
      {"nine",  "9"},
      {"ten",   "10"},
      {"one",   "1"},
      {"two",   "2"},
      {"three", "3"},
      {"four",  "4"},
      {"five",  "5"},
      {"six",   "6"},
      {"seven", "7"},
      {"eight", "8"},
      {"nine",  "9"},
      {"ten",   "10"},
      {"one",   "1"},
      {"two",   "2"},
      {"three", "3"},
      {"four",  "4"},
      {"five",  "5"},
      {"six",   "6"},
      {"seven", "7"},
      {"eight", "8"},
      {"nine",  "9"},
      {"ten",   "10"},
    };
    final Object headers[] = {"English", "Numeric"};
    Runnable runner = new Runnable() 
	{
     		 public void run() 
		{
       			 JFrame frame = new JFrame("Table for Printing");
      			 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       			 final JTable tble = new JTable(rows, headers);
     			 JScrollPane scrollPane = new JScrollPane(table);
        		 frame.add(scrollPane, BorderLayout.CENTER);
        		 JButton button = new JButton("Print the table");
       			 ActionListener printAction = new ActionListener()
			 {
         				 public void actionPerformed(ActionEvent e)
				         {
           					 try 
					        {
              						MessageFormat headerFormat = new MessageFormat("Page {0}");
              						MessageFormat footerFormat = new MessageFormat("- {0} -");
              						table.print(JTable.PrintMode.FIT_WIDTH, headerFormat, footerFormat);
            					} 
					        catch (PrinterException e) 
					       {
              						System.err.println("Error while printing: " + e.getMessage());
            					}
         				 }
        			};
       			        button.addActionListener(printAction);
       			        frame.add(button, BorderLayout.SOUTH);
        			frame.setSize(400, 200);
        			frame.setVisible(true);
      		}
    	};
    EventQueue.invokeLater(runner);
  }
}
Reply With Quote
  #4  
Old 25-11-2009
Member
 
Join Date: May 2008
Posts: 2,373
Re: Print JTable with the help of java

Hi, I have a code which will print JTable to Pdf directly. You just use this code and tell me whether it is working or not. As I don't know java. I have got this code on internet while surfing. So, just try to use it to print your java jtable.

import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.io.FileOutputStream;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;

public class tableprint extends JFrame
{
private JTable tble;
public tableprint()
{
getContentPane().setLayout(new BorderLayout());
createTable();
}
private void createTable()
{
Object[][] row = {{ "a", "b", "e", 4, false } };
String[] column = { "A", "B", "C", "D", "E" };
tble = new JTable(row, column);
JPanel tPanel = new JPanel(new BorderLayout());
tPanel.add(tble.getTableHeader(), BorderLayout.NORTH);
tPanel.add(tble, BorderLayout.CENTER);
getContentPane().add(tPanel, BorderLayout.CENTER);
}
private void print()
{
Document document = new Document(PageSize.A4.rotate());
try
{
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("jTable.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
Graphics2D g2 = cb.createGraphicsShapes(500, 500);
Shape oldClip = g2.getClip();
g2.clipRect(0, 0, 500, 500);
tble.print(g2);
g2.setClip(oldClip);
g2.dispose();
cb.restoreState();
}
catch (Exception e)
{
System.err.println(e.getMessage());
}
document.close();
}
public static void main(String[] args)
{
tableprint frame = new tableprint();
frame.pack();
frame.setVisible(true);
frame.print();
}
}
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Print JTable with the help of java"
Thread Thread Starter Forum Replies Last Post
How to Print Tables using Swings in Java? NIcaBoy Software Development 5 16-02-2010 01:49 AM
Print in java using Swing Sheenas Software Development 3 18-11-2009 08:50 AM
How to retrieve data from a jTable in Java and MS access Corona Software Development 3 09-07-2009 11:59 AM
How to refresh data of Jtable in java sarmad_iu Software Development 1 28-05-2009 02:45 PM
JAVA how do i print 4999 spaces Daren Software Development 2 25-05-2009 07:44 PM


All times are GMT +5.5. The time now is 11:21 AM.