PDA

Click to See Complete Forum and Search --> : Print Jpanel and its jcomponents


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!

LanguageNewbie
Aug 3rd, 2004, 03:56 AM
Sorry accidentally doubleposted. Tried to delete but had an access denied error. :( Sorry please reply in the other thread with the same name and if an admin sees this please delete this thread.

Dillinger4
Aug 3rd, 2004, 09:06 PM
Your other thread has been deleted. You should have been able to delete it. Next time just click the edit button then check the delete box and hit Delete Now.

brown monkey
Aug 3rd, 2004, 10:33 PM
we can't delete now mate Dilenger4, the thread in general. but we can delete a post though. i don't know why. visuaAd told me about a week ago. well of course you can, you're a mod. :)

Dillinger4
Aug 3rd, 2004, 10:50 PM
Sorry. Yeah i forgot about the changes that were recently made. :(