How do I print a JTable?

John Zukowski

JDK 5.0 provides built in support for printing tables. Just call the print() method of JTable. Here's an example of such.

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",   "ichi - u4E00"},
      {"two",   "ni - u4E8C"},
      {"three", "san - u4E09"},
      {"four",  "shi - u56DB"},
      {"five",  "go - u4E94"},
      {"six",   "roku - u516D"},
      {"seven", "shichi - u4E03"},
      {"eight", "hachi - u516B"},
      {"nine",  "kyu - u4E5D"},
      {"ten",   "ju - u5341"},
      {"one",   "ichi - u4E00"},
      {"two",   "ni - u4E8C"},
      {"three", "san - u4E09"},
      {"four",  "shi - u56DB"},
      {"five",  "go - u4E94"},
      {"six",   "roku - u516D"},
      {"seven", "shichi - u4E03"},
      {"eight", "hachi - u516B"},
      {"nine",  "kyu - u4E5D"},
      {"ten",   "ju - u5341"},
      {"one",   "ichi - u4E00"},
      {"two",   "ni - u4E8C"},
      {"three", "san - u4E09"},
      {"four",  "shi - u56DB"},
      {"five",  "go - u4E94"},
      {"six",   "roku - u516D"},
      {"seven", "shichi - u4E03"},
      {"eight", "hachi - u516B"},
      {"nine",  "kyu - u4E5D"},
      {"ten",   "ju - u5341"},
      {"one",   "ichi - u4E00"},
      {"two",   "ni - u4E8C"},
      {"three", "san - u4E09"},
      {"four",  "shi - u56DB"},
      {"five",  "go - u4E94"},
      {"six",   "roku - u516D"},
      {"seven", "shichi - u4E03"},
      {"eight", "hachi - u516B"},
      {"nine",  "kyu - u4E5D"},
      {"ten",   "ju - u5341"},
      {"one",   "ichi - u4E00"},
      {"two",   "ni - u4E8C"},
      {"three", "san - u4E09"},
      {"four",  "shi - u56DB"},
      {"five",  "go - u4E94"},
      {"six",   "roku - u516D"},
      {"seven", "shichi - u4E03"},
      {"eight", "hachi - u516B"},
      {"nine",  "kyu - u4E5D"},
      {"ten",   "ju - u5341"},
      {"one",   "ichi - u4E00"},
      {"two",   "ni - u4E8C"},
      {"three", "san - u4E09"},
      {"four",  "shi - u56DB"},
      {"five",  "go - u4E94"},
      {"six",   "roku - u516D"},
      {"seven", "shichi - u4E03"},
      {"eight", "hachi - u516B"},
      {"nine",  "kyu - u4E5D"},
      {"ten",   "ju - u5341"},
      {"one",   "ichi - u4E00"},
      {"two",   "ni - u4E8C"},
      {"three", "san - u4E09"},
      {"four",  "shi - u56DB"},
      {"five",  "go - u4E94"},
      {"six",   "roku - u516D"},
      {"seven", "shichi - u4E03"},
      {"eight", "hachi - u516B"},
      {"nine",  "kyu - u4E5D"},
      {"ten",   "ju - u5341"},
      {"one",   "ichi - u4E00"},
      {"two",   "ni - u4E8C"},
      {"three", "san - u4E09"},
      {"four",  "shi - u56DB"},
      {"five",  "go - u4E94"},
      {"six",   "roku - u516D"},
      {"seven", "shichi - u4E03"},
      {"eight", "hachi - u516B"},
      {"nine",  "kyu - u4E5D"},
      {"ten",   "ju - u5341"},
      {"one",   "ichi - u4E00"},
      {"two",   "ni - u4E8C"},
      {"three", "san - u4E09"},
      {"four",  "shi - u56DB"},
      {"five",  "go - u4E94"},
      {"six",   "roku - u516D"},
      {"seven", "shichi - u4E03"},
      {"eight", "hachi - u516B"},
      {"nine",  "kyu - u4E5D"},
      {"ten",   "ju - u5341"},
      {"one",   "ichi - u4E00"},
      {"two",   "ni - u4E8C"},
      {"three", "san - u4E09"},
      {"four",  "shi - u56DB"},
      {"five",  "go - u4E94"},
      {"six",   "roku - u516D"},
      {"seven", "shichi - u4E03"},
      {"eight", "hachi - u516B"},
      {"nine",  "kyu - u4E5D"},
      {"ten",   "ju - u5341"},
    };
    final Object headers[] = {"English", "Japanese"};
    Runnable runner = new Runnable() {
      public void run() {
        JFrame frame = new JFrame("Table Printing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JTable table = new JTable(rows, headers);
        JScrollPane scrollPane = new JScrollPane(table);
        frame.add(scrollPane, BorderLayout.CENTER);
        JButton button = new JButton("Print");
        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 pe) {
              System.err.println("Error printing: " + pe.getMessage());
            }
          }
        };
        button.addActionListener(printAction);
        frame.add(button, BorderLayout.SOUTH);
        frame.setSize(300, 150);
        frame.setVisible(true);
      }
    };
    EventQueue.invokeLater(runner);
  }
}
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact