I am trying to print a JPanel that is within an application, the JPanel inside the application is displayed below (Notice the horizontal and vertical scroll bars):
When the application has run it's course, the height of the panel becomes very long,
but when I print it, it does not print the entire width and height.
Also the positioning of the panel in the paper is align to the left rather than center. The Image below shows how the panel appears on paper, this looks very bad so please I need help.
1) How can I print the entire panel?
2) How can I position it to be centralized on the paper?
Here is my code:
public void printWork()
{
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setJobName(" Opt De Solver Printing ");
pj.setPrintable(new Printable()
{
@Override
public int print(Graphics pg, PageFormat pf, int pageNum)
{
if(pageNum > 0)
return Printable.NO_SUCH_PAGE;
Graphics2D g2 = (Graphics2D)pg;
g2.translate(pf.getImageableX(), pf.getImageableY());
MainAppPanel.this.paintAll(g2);
/* I've tried the following commented codes but they don't work */
//MainAppPanel.this.printAll(g2);
//MainAppPanel.this.print(g2);
//MainAppPanel.this.print(g2);
return Printable.PAGE_EXISTS;
}
});
if(pj.printDialog() == false)
return;
try
{
pj.print();
}
catch(PrinterException xcp)
{
xcp.printStackTrace(System.err);
}
}