aquaballoon

Java: Print Table

Feb 25th, 2012
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.41 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.awt.print.*;
  5.  
  6. public class TablePrint {
  7.   public static void main(String args[]) {
  8.     final Object rows[][] = {
  9.       {"one",   "1"},
  10.       {"two",   "2"},
  11.       {"three", "3"},
  12.       {"four",  "4"},
  13.     };
  14.     final Object headers[] = {"English", "#"};
  15.  
  16.     JFrame frame = new JFrame("Table Printing");
  17.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18.  
  19.     final JTable table = new JTable(rows, headers);
  20.     JScrollPane scrollPane = new JScrollPane(table);
  21.     frame.add(scrollPane, BorderLayout.CENTER);
  22.     JButton button = new JButton("Print");
  23.  
  24. //----> the beginning of Print Event
  25.     ActionListener printAction = new ActionListener() {
  26.       public void actionPerformed(ActionEvent e) {
  27.         try {
  28.  
  29.           table.print();
  30.           //MessageFormat headerFormat = new MessageFormat("Page {0}"); // Print title
  31.           //MessageFormat footerFormat = new MessageFormat("- {0} -");  // Print page number
  32.           //table.print(JTable.PrintMode.FIT_WIDTH, headerFormat, footerFormat);
  33.  
  34.         } catch (PrinterException pe) {
  35.           System.err.println("Error printing: " + pe.getMessage());
  36.         }
  37.       }
  38.     };
  39.     button.addActionListener(printAction);
  40. //----> the end of Print Event
  41.  
  42.     frame.add(button, BorderLayout.SOUTH);
  43.     frame.setSize(300, 150);
  44.     frame.setVisible(true);
  45.   }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment