Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

java - Sending the JFrame information to the Printer

The application is for pulling information to fill out a form from a database or write to the database from this form. Right now I can do both of those with using netbeans and contacting my MySQL test server that I have up and running at the moment.

The issue I am having is I need to print the information attained from the database in a form like manner rather than a table, in order to match the hand written forms we are currently using at the office. Is there a way to print the entire JFrame or all the contents in the JFrame just as they are laid out on the screen for the user to see?

Everything that I have seen thus far will print either a region of the screen (text box) or print it via a table.

The application will be compiled for both Linux and Windows when all is said and done.

Code:

package Information;

import java.awt.print.*;
import java.awt.*;
import javax.swing.*;

public class HATDB extends javax.swing.JFrame implements Printable {

    JFrame frameToPrint;

    /** Creates new form HATDB */
    public HATDB() {
    }

    @Override
    public int print(Graphics g, PageFormat pf, int page) throws
        PrinterException {

        if (page > 0) { /* We have only one page, and 'page' is zero-based */
            return NO_SUCH_PAGE;
        }

        /* User (0,0) is typically outside the imageable area, so we must
         * translate by the X and Y values in the PageFormat to avoid clipping
         */
        Graphics2D g2d = (Graphics2D) g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());

        /* Now print the window and its visible contents */
        frameToPrint.printAll(g);

        /* tell the caller that this page is part of the printed document */
        return PAGE_EXISTS;
    }

    public HATDB(JFrame f) {
        frameToPrint = f;
    }

    private void OK_ButtonActionPerformed(java.awt.event.ActionEvent evt) {
        PrinterJob job = PrinterJob.getPrinterJob();
        //job.setPrintable();
        boolean ok = job.printDialog();
        if (ok) {
            try {
                job.print();
            } catch (PrinterException ex) {
                ex.printStackTrace(System.err);
            }
        }

    }

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */

        JFrame f = new JFrame("Print UI Example");
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new HATDB().setVisible(true);
            }
        });
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From the JTable API: "J2SE 5 adds methods to JTable to provide convenient access to some common printing needs. Simple new print() methods allow for quick and easy addition of printing support to your application." These methods print all the rows in your TableModel, rather than just those rows that are visible.

Addendum: You can print exactly what's on the screen, as shown in Printing the Contents of a User Interface, or you can print the entire contents of the TableModel, as shown here and in Chapter 6 Continued: Advanced Printing.

Addendum: Here's an example that prints a JPanel with a JTree.

import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import javax.swing.*;

/** @see http://stackoverflow.com/questions/8192204 */
public class PrintTest extends JPanel implements Printable {

    public PrintTest() {
        this.setLayout(new GridLayout());
        JTree tree = new JTree();
        this.add(new JScrollPane(tree));
        for (int i = 0; i < tree.getRowCount(); i++) {
            tree.expandRow(i);
        }
    }

    @Override
    public int print(Graphics g, PageFormat pf, int i) throws PrinterException {
        if (i > 0) {
            return NO_SUCH_PAGE;
        }
        Graphics2D g2d = (Graphics2D) g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());
        PrintTest.this.printAll(g);
        return Printable.PAGE_EXISTS;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                final PrintTest pt = new PrintTest();
                f.add(pt, BorderLayout.CENTER);
                JButton b = new JButton(new AbstractAction("Print") {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        PrinterJob pj = PrinterJob.getPrinterJob();
                        PageFormat pf = pj.pageDialog(pj.defaultPage());
                        pj.setPrintable(pt, pf);
                        if (pj.printDialog()) {
                            try {
                                pj.print();
                            } catch (PrinterException pe) {
                                pe.printStackTrace(System.err);
                            }
                        }
                    }
                });
                JPanel p = new JPanel();
                p.add(b);
                f.add(p, BorderLayout.SOUTH);
                f.pack();
                f.setVisible(true);
            }
        });
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...