Tuesday, September 14, 2010

java Printing

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") //
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(); }
//
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());

SP.print(g);
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
}

No comments:

Post a Comment

Tnx for ur comment