Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: AWT
Expertise: Beginner
Mar 31, 1998

Printing Components

Question:
How do I print out a component?

Answer:
Java 1.1 introduced the java.awt.PrintJob and java.awt.PrintGraphics classes. It also added the print(Graphics) and printAll(Graphics) methods to java.awt.Component, and the printComponents(Graphics) method to java.awt.Container. To print a component, you really ony need to concern yourself with the PrintJob class and the print(Graphics) method in Component.

The procedure for printing a component is to create a PrintJob instance, obtain its drawing context through getGraphics(), use that as an argument to print(Graphics), and end the print job by calling the PrintJob end() method. The only tricky part of this procedure is to create a PrintJob instance. You do not use the new operator to do that. Rather, you get the default Toolkit instance, using Toolkit.getDefaultToolkit(), and then create a PrintJob through getPrintJob(Frame, String, Properties). The Toolkit class mediates between the pure Java environment and the system dependent facilities.

Below is an example of a button that prints itself. Note that you should call print(), and not printComponents(). Also, on most systems, getPrintJob() will block, presenting the user with a print configuration dialog. If the user dismisses the dialog by pressing the cancel button, getPrintJob() will return null, which is why you should check the return value before using it.

import java.awt.*;
import java.awt.event.*;

import com.sun.java.swing.*;


public final class Print {

  public static final void main(String[] args) {
    WindowListener exitListener;
    final JFrame frame = new JFrame();
    final JButton button = new JButton("Print");

    exitListener = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        Window window;
        window = e.getWindow();
        window.setVisible(false);
        window.dispose();
        System.exit(0);
      }
    };                               

    button.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
	if(e.getSource() == button) {
	  PrintJob job;
	  Graphics g;

	  job = Toolkit.getDefaultToolkit().getPrintJob(frame, "A Print Job",
							null);
	  if(job == null) {
	    JOptionPane pane;
	    JOptionPane.showMessageDialog(frame, "Could not get print job!",
					  "Error", JOptionPane.ERROR_MESSAGE); 
	    return;
	  }

	  g = job.getGraphics();
	  frame.print(g);
	  job.end();
	}
      }
    });

    frame.getContentPane().add(button);
    frame.addWindowListener(exitListener);
    frame.pack();
    frame.show();
  }

}
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date