This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Customer Requirements for Developers and have Marcho Behler on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Hang on printing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Hang on printing" Watch "Hang on printing" New topic

Hang on printing

michael yue
Ranch Hand

Joined: Nov 20, 2003
Posts: 204
Hi all,

I am facing a problem with printing. I am trying to straight away print something without generating the file first. Problem is the data to print can be very long horizontally. When the print dialog box pop up and i click ok, it just hangs there. when i delete some of the data to print, it appears fine. Is there anything i can do to prevent this printing hang. My sample code is below. Thanks for the help

// Print a file into the text area.
class PrintCommand implements ActionListener {
public void actionPerformed (ActionEvent e) {
PrintJob pjob = getToolkit().getPrintJob(FrmPrint.this, "printRpt", p);
if (pjob != null) {
Graphics pg = pjob.getGraphics();
if (pg != null) {
String s = textArea.getText();
printLongString (pjob, pg, s);

// I'm assuming a one-inch margin on all
// four sides. This could be done better.
private int margin = 72;

// Print string to graphics via printjob
// Does not deal with word wrap or tabs
private void printLongString (PrintJob pjob, Graphics pg, String s) {

int pageNum = 1;
int linesForThisPage = 0;
int linesForThisJob = 0;
// Note: String is immutable so won't change while printing.
if (!(pg instanceof PrintGraphics)) {
throw new IllegalArgumentException ("Graphics context not PrintGraphics");
StringReader sr = new StringReader (s);
LineNumberReader lnr = new LineNumberReader (sr);
String nextLine;
int pageHeight = pjob.getPageDimension().height - margin;
Font helv = new Font("Courier", Font.PLAIN, 10);//Helvetica
//have to set the font to get any output
pg.setFont (helv);
FontMetrics fm = pg.getFontMetrics(helv);
int fontHeight = fm.getHeight();
int fontDescent = fm.getDescent();
int curHeight = margin;
try {
do {
nextLine = lnr.readLine();
if (nextLine != null) {
if ((curHeight + fontHeight) > pageHeight) {
// New Page
System.out.println ("" + linesForThisPage + " lines printed for page " + pageNum);
if (linesForThisPage == 0) {
System.out.println ("Font is too big for pages of this size; aborting...");
linesForThisPage = 0;
pg = pjob.getGraphics();
if (pg != null) {
pg.setFont (helv);
curHeight = 0;
curHeight += fontHeight;
if (pg != null) {
pg.drawString (nextLine, margin, curHeight - fontDescent);

} else {
System.out.println ("pg null");
} while (nextLine != null);
} catch (EOFException eof) {
// Fine, ignore
} catch (Throwable t) { // Anything else
System.out.println ("" + linesForThisPage + " lines printed for page " + pageNum);
System.out.println ("pages printed: " + pageNum);
System.out.println ("total lines printed: " + linesForThisJob);
Consider Paul's rocket mass heater.
subject: Hang on printing
It's not a secret anymore!