Example usage for javax.swing JTable print

List of usage examples for javax.swing JTable print

Introduction

In this page you can find the example usage for javax.swing JTable print.

Prototype

public boolean print(PrintMode printMode, MessageFormat headerFormat, MessageFormat footerFormat)
        throws PrinterException 

Source Link

Document

A convenience method that displays a printing dialog, and then prints this JTable in the given printing mode, with the specified header and footer text.

Usage

From source file:TablePrintMessageFormat.java

public static void main(String args[]) {
    final Object rows[][] = { { "one", "1" }, { "two", "2" }, { "three", "3" }, { "four", "4" }, { "one", "1" },
            { "two", "2" }, { "three", "3" }, { "four", "4" }, { "one", "1" }, { "two", "2" }, { "three", "3" },
            { "four", "4" }, { "one", "1" }, { "two", "2" }, { "three", "3" }, { "four", "4" },

    };//from  w w  w .  j  a  v  a 2  s  .c o  m
    final Object headers[] = { "English", "#" };

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

From source file:classes.SharedClass.java

public static void print(String message, JTable table) {
    MessageFormat header = new MessageFormat(message);
    MessageFormat footer = new MessageFormat("(0,number,nteger)");
    try {//w w  w  .  j a v a 2 s.  c  o  m
        boolean result = table.print(JTable.PrintMode.FIT_WIDTH, header, footer);
        if (result) {
            JOptionPane.showMessageDialog(null, "   ");
        }
    } catch (PrinterException ex) {
        Logger.getLogger(SharedClass.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:Software_Jframes.chart.java

void auto_print(JTable jTable5) {
    try {//  w ww . j  a va2  s .c  o m
        MessageFormat header = new MessageFormat("Report Print");

        MessageFormat footer = new MessageFormat("Page{0,number,integer}");
        jTable5.print(JTable.PrintMode.NORMAL, header, footer);
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Cannot Print" + e);
    }
}