Hello.
I have a problem to print my JFrame, i've read the tutorials A Basic Printing Program (The Java™ Tutorials > 2D Graphics > Printing)
But i stil dont get it.
When printing I get a new jframe,with a different look and like. Guess it has to do with new gui(), but "job.setPrintable(new gui(), pf)" won't take this as parameter?.
Heres my code:
public class gui extends JFrame implements Printable{ ... ... ... public gui(){ ... ... printAction.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ PrinterJob job = PrinterJob.getPrinterJob(); PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); PageFormat pf = job.pageDialog(aset); job.setPrintable(new gui(), pf); boolean ok = job.printDialog(aset); if (ok) { try { job.print(aset); } catch (PrinterException ex) { /* The job did not successfully complete */ } } } } ); public int print(Graphics g, PageFormat pf, int page) throws PrinterException{ if (page > 0) { return NO_SUCH_PAGE; } Graphics2D g2d = (Graphics2D)g; g2d.translate(pf.getImageableX(), pf.getImageableY()); // Print the entire visible contents of a // java.awt.Frame. this.printAll(g); return PAGE_EXISTS; } } }
Sens.