Printing Components

You don't draw most buttons, lists, scrollbars, panels, and so on in your paint() method. You don't have to print them there either. The java.awt.Component class has print() and printAll() methods

 public void print(Graphics g)
 public void printAll(Graphics g)
java.awt.Container adds a printComponents() method that prints all components in the container:

 public void printComponents(Graphics g)

For example, to print a Frame f, you might do this:

  Toolkit tk = f.getToolkit();
  Printjob pj = tk.getPrintJob(this, getTitle(), null);
  if (pj != null) {
    Graphics pg = pj.getGraphics();
    f.printComponents(pg);
    pg.dispose();
    pj.end();
  }

However this isn't always what you want since printComponents() only prints the visible area. for example, in the text editing application the TextArea may only show part of the text at one time, but you really want to print all the text.


Previous | Next | Top
Last Modified April 19, 1997
Copyright 1997 Elliotte Rusty Harold
elharo@metalab.unc.edu