LanguageNewbie
Aug 3rd, 2004, 02:38 AM
Hi im having problems trying to print a jpanel and all the jcomponents inside it(jlabels, jtextarea etc). Im having problems trying to print it in landscape format. the width seems to be cut halfway when trying to print it in landscape. Works fine in portrait though.
Apparently all settings changed in the page and printer dialog that pops up doesnt seem to work for the page that im trying to print too.
public class PrintUtilities implements Printable {
private Component componentToBePrinted;
public static void printComponent(Component c) {
new PrintUtilities(c).print();
}
public PrintUtilities(Component componentToBePrinted) {
this.componentToBePrinted = componentToBePrinted;
}
public void print() {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
PageFormat pageFormat = printJob.pageDialog(printJob.defaultPage());
if (printJob.printDialog())
try {
printJob.print();
} catch(PrinterException pe) {
System.out.println("Error printing: " + pe);
}
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
if (pageIndex > 0) {
return(NO_SUCH_PAGE);
} else {
Graphics2D g2d = (Graphics2D)g;
pageFormat.setOrientation(PageFormat.LANDSCAPE);
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
disableDoubleBuffering(componentToBePrinted);
componentToBePrinted.paint(g2d);
enableDoubleBuffering(componentToBePrinted);
return(PAGE_EXISTS);
}
The above is close to the entire class which i use for printing out the jpanel. Its not my work im just trying to modify it to work for me.(credits to Marty Hall, http://www.apl.jhu.edu/~hall/java/).
The code that is used to print the jpanel is
PrintUtilities.printComponent(jPanel);
Please help thanks in advance!
Apparently all settings changed in the page and printer dialog that pops up doesnt seem to work for the page that im trying to print too.
public class PrintUtilities implements Printable {
private Component componentToBePrinted;
public static void printComponent(Component c) {
new PrintUtilities(c).print();
}
public PrintUtilities(Component componentToBePrinted) {
this.componentToBePrinted = componentToBePrinted;
}
public void print() {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
PageFormat pageFormat = printJob.pageDialog(printJob.defaultPage());
if (printJob.printDialog())
try {
printJob.print();
} catch(PrinterException pe) {
System.out.println("Error printing: " + pe);
}
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
if (pageIndex > 0) {
return(NO_SUCH_PAGE);
} else {
Graphics2D g2d = (Graphics2D)g;
pageFormat.setOrientation(PageFormat.LANDSCAPE);
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
disableDoubleBuffering(componentToBePrinted);
componentToBePrinted.paint(g2d);
enableDoubleBuffering(componentToBePrinted);
return(PAGE_EXISTS);
}
The above is close to the entire class which i use for printing out the jpanel. Its not my work im just trying to modify it to work for me.(credits to Marty Hall, http://www.apl.jhu.edu/~hall/java/).
The code that is used to print the jpanel is
PrintUtilities.printComponent(jPanel);
Please help thanks in advance!