Los Alamos National Laboratory

D  Decision Applications


  • Group Leader
    Kristin Omberg
  • Deputy Group Leader
    David Cremer
  • Office Administrator
    Lorraine Johnson
  • Group Office Location
    TA-52, Bldg. 115
    MS K551


When you read the comments for the PrintUtilities class, you can see that it has gone through a number of hands to reach its current state. The Java™ print API is a little difficult to work with, so hopefully we can give back to the community a little with this class. This class could use some work. For instance, many calculations are repeated for every page. But this class does print a Java™ component, with page breaks at places between components. The printed page has a header and footer. A subheader can have any text, and the subfooter has the text “Page 1 of 7 pages,” for example. To print a Java Component:

import java.awt.print.PageFormat;
Component comp = new Component(); // A Java Component, such as a JPanel, or Frame
int [] pageBreaks; // this is an integer array. Each value gives the possible location of a
// page break
String batchNumber = “This is the text of the subheader printed on each page”;
PrintUtilities.printComponent( comp, pageBreaks, batchNumber,
PageFormat.PORTRAIT); // can also use PageFormat.LANDSCAPE

The page breaks array can be generated by getting the location of the bottom of each GUI element displayed in the Component. If all of the components are located in rows, then this method could be used to generate the page breaks:

import java.awt.Container;
import java.awt.Rectangle;

public int [] getPageBreaks(Container c)
int [] pageBreaks = new int[c.getComponentCount()];
Rectangle rec;
for(int i=0; i<pageBreaks.length; i++)
rec = c.getComponent[i].getBounds();
pageBreaks[i] = rec.y + rec.height;
return pageBreaks;

download button (java utilities.zip)

Operated by Los Alamos National Security, LLC for the U.S. Department of Energy's NNSA
Inside | © Copyright 2014 Los Alamos National Security, LLC All rights reserved | Disclaimer/Privacy | Web Contact