|
-
September 25th, 2000, 10:01 AM
#1
Print contents of JPanel
Need to print the contents of my main JPanel to printer. It contains a JList with image icons and text.
-
September 29th, 2000, 03:33 PM
#2
Re: Print contents of JPanel
I figured it out...here's the code:
For your action code:
public void actionPerformed(ActionEvent e) {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(yourPanel.this);
if (printJob.printDialog()) {
try {
printJob.print();
} catch (Exception prt) {
System.err.println(prt.getMessage());
}
}
}
And include a print method in your code:
public int print(Graphics g, PageFormat pf, int pageIndex) {
if (pageIndex != 0) return NO_SUCH_PAGE;
Graphics2D g2 = (Graphics2D)g;
g2.translate(pf.getImageableX(), pf.getImageableY());
paint(g2);
return PAGE_EXISTS;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|