nevermind, it printed.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
nevermind, it printed.
still cant figure out how to print. i dont understand java well enough to implement PrintUIWindow into my program.
The print() method in the PrintUIWindow program is called with a Graphics object that will contain what will be printed. In a GUI program the paintComponent() method is called with a Graphics object that will contain what will be displayed on the screen.
The code that uses the Graphic object's draw...() methods will be the same code that is used to display the GUI and to print the GUI.
In your program, write a drawing method that takes a Graphics object and draws what you want to see.
Override the paintComponent() method for the container its to be shown in and in paintComponent() call your method with the Graphics object. Once you have the drawing method working, then use the code in PrintUIWindow to call YOUR drawing method with the Graphics object vs using the statement thats now in the program: frameToPrint.printAll(g)
Here's a sample program:import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.print.*; public class ReportPrinter implements Printable, ActionListener { // The Printable interface public int print(Graphics g, PageFormat pf, int page) throws PrinterException { if (page > 0) { /* We have only one page, and 'page' is zero-based */ return NO_SUCH_PAGE; } /* User (0,0) is typically outside the imageable area, so we must * translate by the X and Y values in the PageFormat to avoid clipping */ Graphics2D g2d = (Graphics2D)g; g2d.translate(pf.getImageableX(), pf.getImageableY()); /* Now we perform our rendering */ fillInPanel(g); // Call our method to draw the panel /* tell the caller that this page is part of the printed document */ return PAGE_EXISTS; } // ----------------------------------------------------- public void actionPerformed(ActionEvent e) { PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(this); boolean ok = job.printDialog(); if (ok) { try { job.print(); } catch (PrinterException ex) { ex.printStackTrace(); /* The job did not successfully complete */ } } } // Build the report using the Graphics for the panel public static void fillInPanel(Graphics g) { g.drawString("Hello world!", 10, 20); g.drawString("Another message", 10, 40); g.drawString("More Stuff", 10, 60); g.drawString("And more", 10, 80); } // Start here - create frame and fill in panel public static void main(String args[]) { UIManager.put("swing.boldMetal", Boolean.FALSE); JFrame f = new JFrame("Hello World Printer"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); JButton printButton = new JButton("Print This"); printButton.addActionListener(new ReportPrinter()); f.add(printButton, BorderLayout.SOUTH); JPanel jp = new JPanel() { public void paintComponent(Graphics g) { super.paintComponent(g); fillInPanel(g); // fill in panel } }; f.add(jp,BorderLayout.CENTER); f.setSize(200, 200); f.setVisible(true); } }