Sending JFrame information to a printer

The application is designed to pull information to fill out a form from a database or write to a database from this form. Right now I can get along with those who use netbeans and turned to my MySQL test server, which I have and is working at the moment.

The problem I am facing is that I need to print the information received from the database in a form similar to the manner, and not a table, in order to match the manually written forms that we now use in the office. Is there a way to print all of the JFrame, or all of the content in the JFrame, just as they are laid out on the screen for viewing by the user?

Everything that I have seen so far will either print the screen area (text box) or print it through a table.

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

the 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); } }); } } 
0
source share
1 answer

From the JTable API: "J2SE 5 adds methods to JTable to provide convenient access to some common printing needs. print() allows you to quickly and easily add print support to your application." These methods print all the rows in the TableModel , not just the ones that are visible.

Appendix: you can print exactly what is displayed on the screen, as shown in Printing the contents of the user interface , or you can print all the contents of the TableModel , as shown here and in Chapter 6 Continued: Advanced Printing .

Appendix: Here is an example that prints a JPanel with 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); } }); } } 
+3
source

All Articles