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.
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.
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); } }
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); } }
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();
}
}
Bookmarks