-
Print jFrame
Hi - I'm designing a small jFrame with some labels, fields and a table. I want to send this frame to a printer so that i can take a hard copy of the the frame with all the contents. I'm using netbeans as the ide. Highly appreciate any help on this.
Thanks in advanced.
Uthpala
-
I don't think there is a simple/direct way to do is.
What you can do is, take screen shot of your UI and print it. Read more about java.awt.print for printing a document.
-
i've already seen code that can print components, for example here Swing Tutorial: High-Quality Java Printing but i never check it out. if you try it out could you please give a short information if it works?
-
It use the java.awt.print. Nothing else. ;p
-
hey guys... i could find a code which is printing a gui form with all components.. it's working fine..
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.print.*;
class PrintUIWindow implements Printable, ActionListener {
JFrame frameToPrint;
public int print(Graphics g, PageFormat pf, int page) throws
PrinterException {
if (page > 0) {
return NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY()-55);
frameToPrint.print(g);
return PAGE_EXISTS;
}
public void actionPerformed(ActionEvent e) {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
boolean ok = job.printDialog();
if (ok) {
try {
job.print();
} catch (PrinterException ex) {
}
}
}
public PrintUIWindow(JFrame f) {
frameToPrint = f;
}
public static void main(String args[]) {
UIManager.put("swing.boldMetal", Boolean.FALSE);
JFrame f = new JFrame("Print UI Example");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
JLabel label1=new JLabel("Selling Bill",JLabel.CENTER);
JLabel label2=new JLabel("Customer Name :Mahmoud Saleh ",JLabel.LEFT);
JLabel label3=new JLabel("Buying Date :29/8/2008 ",JLabel.LEFT);
JLabel label4=new JLabel("Book Buyed :Java Printing ",JLabel.LEFT);
JLabel label5=new JLabel("Number : 6 Copies ",JLabel.LEFT);
JLabel label6=new JLabel("Total Price :600 $ ",JLabel.LEFT);
label1.setFont(new Font("Courier New", Font.BOLD, 13));
label2.setFont(new Font("Courier New", Font.BOLD, 13));
label3.setFont(new Font("Courier New", Font.BOLD, 13));
label4.setFont(new Font("Courier New", Font.BOLD, 13));
label5.setFont(new Font("Courier New", Font.BOLD, 13));
label6.setFont(new Font("Courier New", Font.BOLD, 13));
JButton printButton = new JButton("Print This Window");
printButton.addActionListener(new PrintUIWindow(f));
JPanel panel=new JPanel();
panel.setLayout(new GridLayout(6,1));
panel.add(label1);
panel.add(label2);
panel.add(label3);
panel.add(label4);
panel.add(label5);
panel.add(label6);
f.setSize(300,300);
f.setLocationRelativeTo(null);
f.add(panel,BorderLayout.CENTER);
f.add(printButton,BorderLayout.SOUTH);
panel.setBackground(Color.WHITE);
f.setResizable(false);
f.setVisible(true);
}
}
--------------------------------------------------------------------
and i also created a small frame using netbeans and it's also working..
hope this will help u guys too....
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* SimplePrint.java
*
* Created on Jan 11, 2010, 4:29:16 PM
*/
package home.tess.ukw;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionListener;
import java.awt.print.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
*
* @author uthpalaw
*/
public class SimplePrint extends javax.swing.JFrame implements Printable {
JFrame frameToPrint;
static JFrame SP = new SimplePrint();
/** Creates new form SimplePrint */
public SimplePrint() {
initComponents();
}
public SimplePrint(JFrame f){
frameToPrint = f;
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Simple Print Form");
jLabel1.setText("Hello...");
jButton1.setText("Print");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addContainerGap(361, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(51, 51, 51)
.addComponent(jLabel1)
.addGap(65, 65, 65)
.addComponent(jButton1)
.addContainerGap(132, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
// JOptionPane.showMessageDialog(null, "hello");
PrinterJob PJ = PrinterJob.getPrinterJob();
//PageFormat PF = PJ.pageDialog(PJ.defaultPage());
PJ.setPrintable((Printable) this);
boolean doPrint = PJ.printDialog();
//JOptionPane.showMessageDialog(null, doPrint);
if(doPrint){
try {
PJ.print();
} catch (PrinterException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageIndex > 0) {
return(NO_SUCH_PAGE);
} else {
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
// Turn off double buffering
//componentToBePrinted.paint(g2d);
//frameToPrint.print(g);
SP.print(g);
// Turn double buffering back on
return(PAGE_EXISTS);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
SP.setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
-
Fine, if you are satisfied with what you have mean you found the solution. If so please mark the thread solved. And also please don't forget to use code tags next time. Good luck! :)
-
Could you please show me how to make this thread as solved. ?
-
On top of the thread, you can find a menu. Click on Thread Tools there, you can find the option to mark the thread solved.