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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…