I’m working with a Java GUI and I want to get a single panel to print. I get the prompt to come up, and I get something to print, but the problem is that something is just a blank page.
Below is all the relevant code:
public class DrawPanel extends JPanel implements MouseMotionListener, Printable {
…
public void print(){
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
if(job.printDialog()){
try{
job.print();
}catch(PrinterException e){
System.err.print("Print failed: " + e);}
}
}
…
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
throws PrinterException {
if(pageIndex > 0){
return NO_SUCH_PAGE;
}
Graphics2D g = (Graphics2D)graphics;
g.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
this.setDoubleBuffered(false);
this.paint(g);
return PAGE_EXISTS;
}
And then in another object which calls the function:
if(evt.getSource() == btn_print){
p.print();
}
1 Answer