For Programmers: Free Programming Magazines  


Home > Archive > Java Help > October 2004 > printing problem - blank page









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author printing problem - blank page
Vince

2004-10-24, 3:56 am

I have included some printing code that compiles but only prints blank
pages. Does anyone have an explanation?
Warning: If you run this, a print dialog box will appear. Be sure to limit
the printed pages to 1.
Vince

Here is the code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.awt.print.*;
class tryToPrint extends JFrame implements ActionListener, Printable
{
JButton btn=new JButton("print");
JPanel panel_1=new JPanel();
JLabel lbl=new JLabel("text");

tryToPrint() {
super("Try to Print");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

Container contentArea=getContentPane();

panel_1.add(lbl);
panel_1.add(btn);
btn.addActionListener(this);
contentArea.add(panel_1, BorderLayout.CENTER);

setContentPane(contentArea);

}

public void actionPerformed(ActionEvent event){
if(event.getSource()==btn){
try {

PrinterJob job=PrinterJob.getPrinterJob();
job.setPrintable(this);
if (job.printDialog()){
try {job.print();}

catch (Exception e)
{;}
}
}
catch (Exception e)
{;}
}
}

public int print(Graphics g, PageFormat pf, int pageIndex){
return Printable.PAGE_EXISTS;
}
public static void main(String[] args){
tryToPrint tryAgain=new tryToPrint();
}
}


Paul H. van Rossem

2004-10-24, 8:56 am

The explanation is simple: inside the print() your are painting nothing!
And since you always return PAGE_EXISTS (for any page), it keeps
printing empty pages forever.

I suggest you get yourself a good book on Java and read the passage on
printing...

Good luck, Paul.

On 24-10-2004 09:11, Vince wrote:
> I have included some printing code that compiles but only prints blank
> pages. Does anyone have an explanation?
> Warning: If you run this, a print dialog box will appear. Be sure to limit
> the printed pages to 1.
> Vince
>
> Here is the code:
>
> import javax.swing.*;
> import java.awt.*;
> import java.awt.event.*;
> import java.text.*;
> import java.awt.print.*;
> class tryToPrint extends JFrame implements ActionListener, Printable
> {
> JButton btn=new JButton("print");
> JPanel panel_1=new JPanel();
> JLabel lbl=new JLabel("text");
>
> tryToPrint() {
> super("Try to Print");
> setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> setVisible(true);
>
> Container contentArea=getContentPane();
>
> panel_1.add(lbl);
> panel_1.add(btn);
> btn.addActionListener(this);
> contentArea.add(panel_1, BorderLayout.CENTER);
>
> setContentPane(contentArea);
>
> }
>
> public void actionPerformed(ActionEvent event){
> if(event.getSource()==btn){
> try {
>
> PrinterJob job=PrinterJob.getPrinterJob();
> job.setPrintable(this);
> if (job.printDialog()){
> try {job.print();}
>
> catch (Exception e)
> {;}
> }
> }
> catch (Exception e)
> {;}
> }
> }
>
> public int print(Graphics g, PageFormat pf, int pageIndex){
> return Printable.PAGE_EXISTS;
> }
> public static void main(String[] args){
> tryToPrint tryAgain=new tryToPrint();
> }
> }
>
>

Vince

2004-10-24, 3:57 pm


All the examples I have seen use print(). It is confusing, but they suggest
that the printing system then calls from the printable interface the method:
int print(Graphics g, PageFormat pf, int pageIndex){}. Instead, print() is
a method in the PrinterJob class.

I tried job.print(this), but the compiler says "cannot resolve symbol". I
have seen some examples on the web that use print(). An example follows.
( I have looked but have not found a book yet that explains basic printing
very well.)
import java.awt.*;

import java.awt.print.*;

/**

* This wrapper class encapsulates a Component and allows it to be printed

* using the Java 1.2 printing API

*/

public class PrintableComponent implements Printable {

// The component to be printed

Component c;

/** Create a PrintableComponent wrapper around a Component */

public PrintableComponent(Component c) { this.c = c; }

/**

* This method is not part of the Printable interface. It is a method

* that sets up the PrinterJob and initiates the printing.

*/

public void print() throws PrinterException {

// Get the PrinterJob object

PrinterJob job = PrinterJob.getPrinterJob();

// Get the default page format, then allow the user to modify it

PageFormat format = job.pageDialog(job.defaultPage());

// Tell the PrinterJob what to print

job.setPrintable(this, format);

// Ask the user to confirm, and then begin the printing process

if (job.printDialog())

job.print();

}

/**

* This is the "callback" method that the PrinterJob will invoke.

* This method is defined by the Printable interface.

*/

public int print(Graphics g, PageFormat format, int pagenum) {

// The PrinterJob will keep trying to print pages until we return

// this value to tell it that it has reached the end

if (pagenum > 0)

return Printable.NO_SUCH_PAGE;

// We're passed a Graphics object, but it can always be cast to Graphics2D

Graphics2D g2 = (Graphics2D) g;

// Use the top and left margins specified in the PageFormat Note

// that the PageFormat methods are poorly named. They specify

// margins, not the actual imageable area of the printer.

g2.translate(format.getImageableX(), format.getImageableY());

// Tell the Component to draw itself to the printer by passing in

// the Graphics2D object. This will not work well if the Component

// has double-buffering enabled.

c.paint(g2);

// Return this constant to tell the PrinterJob that we printed the page

return Printable.PAGE_EXISTS;

}

}


"Paul H. van Rossem" <paul@timeware.nl> wrote in message
news:417b7db8$0$568$e4fe514c@news.xs4all.nl...
> The explanation is simple: inside the print() your are painting nothing!
> And since you always return PAGE_EXISTS (for any page), it keeps
> printing empty pages forever.
>
> I suggest you get yourself a good book on Java and read the passage on
> printing...
>
> Good luck, Paul.



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com