Results 1 to 4 of 4

Thread: Print JTable with the help of java

  1. #1
    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.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    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);
    }
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    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);
      }
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

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

Similar Threads

  1. How to Print Tables using Swings in Java?
    By NIcaBoy in forum Software Development
    Replies: 5
    Last Post: 16-02-2010, 02:49 AM
  2. Print in java using Swing
    By Sheenas in forum Software Development
    Replies: 3
    Last Post: 18-11-2009, 09:50 AM
  3. How to retrieve data from a jTable in Java and MS access
    By Corona in forum Software Development
    Replies: 3
    Last Post: 09-07-2009, 11:59 AM
  4. How to refresh data of Jtable in java
    By sarmad_iu in forum Software Development
    Replies: 1
    Last Post: 28-05-2009, 02:45 PM
  5. JAVA how do i print 4999 spaces
    By Daren in forum Software Development
    Replies: 2
    Last Post: 25-05-2009, 07:44 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,708,457.09544 seconds with 17 queries