Java JTable print with page header/footer settings

Description

Java JTable print with page header/footer settings

import java.awt.print.PrinterException;
import java.text.MessageFormat;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class Main {
  public static void main(String[] args) {
    JFrame f = new JFrame("JTable example");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //w  ww .  j av a2  s  . c o  m
    String[] columnNames = { "Ordinal" };
    String[][] tableData = {{ "One" }, //
                            { "Two" },//
                            { "Three" } };
    
    JTable table = new JTable(tableData, columnNames);
    
    f.add(new JScrollPane(table));
    f.pack();
    
    f.setVisible(true);


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



PreviousNext

Related