Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.print.*;
- public class TablePrint {
- public static void main(String args[]) {
- final Object rows[][] = {
- {"one", "1"},
- {"two", "2"},
- {"three", "3"},
- {"four", "4"},
- };
- 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");
- //----> the beginning of Print Event
- ActionListener printAction = new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try {
- table.print();
- //MessageFormat headerFormat = new MessageFormat("Page {0}"); // Print title
- //MessageFormat footerFormat = new MessageFormat("- {0} -"); // Print page number
- //table.print(JTable.PrintMode.FIT_WIDTH, headerFormat, footerFormat);
- } catch (PrinterException pe) {
- System.err.println("Error printing: " + pe.getMessage());
- }
- }
- };
- button.addActionListener(printAction);
- //----> the end of Print Event
- frame.add(button, BorderLayout.SOUTH);
- frame.setSize(300, 150);
- frame.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment