Results 1 to 2 of 2
  1. #1
    Paul_White is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default Printing the current window

    I am hoping that this is a easy one.

    I have written an app which once the various fields completed by the user needs to be printed.

    I have a jmenubar at the top with the usual "File" jmenu top left which when clicked drops down a list of Jmenuitems.

    I am calling other classes on most items in the list but the one I am stuck on is how to print.

    The layoout of the screen is perfect and just needs to be sent "as is" to the printer.

    I have set the listener on the jmenuitem thus:

    JMenuBar menuBar = new JMenuBar();
    menuBar.setBounds(0, 0, 1094, 19);
    desktopPane.add(menuBar);

    JMenu mnFile = new JMenu("File");
    menuBar.add(mnFile);

    mntmPrint = new JMenuItem("Print");
    mntmPrint.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseReleased(MouseEvent e) {
    PrintUtilities.printComponent(componentToBePrinted )
    }
    });
    mnFile.add(mntmPrint);

    And I am using this class to do the printing.

    package Navigation;

    import java.awt.*;
    import javax.swing.*;
    import java.awt.print.*;

    /** A simple utility class that lets you very simply print
    * an arbitrary component. Just pass the component to the
    * PrintUtilities.printComponent. The component you want to
    * print doesn't need a print method and doesn't have to
    * implement any interface or do anything special at all.
    * <P>
    * If you are going to be printing many times, it is marginally more
    * efficient to first do the following:
    * <PRE>
    * PrintUtilities printHelper = new PrintUtilities(theComponent);
    * </PRE>
    * then later do printHelper.print(). But this is a very tiny
    * difference, so in most cases just do the simpler
    * PrintUtilities.printComponent(componentToBePrinted ).
    *
    * 7/99 Marty Hall, Java Programming Resources -- Java, Java, and more Java
    * May be freely used or adapted.
    */

    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);
    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;
    g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
    disableDoubleBuffering(componentToBePrinted);
    componentToBePrinted.paint(g2d);
    enableDoubleBuffering(componentToBePrinted);
    return(PAGE_EXISTS);
    }
    }

    /** The speed and quality of printing suffers dramatically if
    * any of the containers have double buffering turned on.
    * So this turns if off globally.
    * @see enableDoubleBuffering
    */
    public static void disableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(false);
    }

    /** Re-enables double buffering globally. */

    public static void enableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(true);
    }
    }

    My problem is that I get the following error.

    The method setPrintable(Printable) in the type PrinterJob is not applicable for the arguments (new MouseAdapter(){})

    I am new to Java and just don't know where to start to solve this problem.

    Please could somebody point me in the right direction.

    Thank you very much.

    Paul
    Last edited by Paul_White; 08-17-2011 at 01:12 PM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,787
    Blog Entries
    7
    Rep Power
    21

    Default

    Get a PrinterJob (read the API documentation for that class) and feed it an implementation of a Printable (it's an interface). The Printable receives a (special) Graphics2D object and it should make your JComponent paint itself on the Graphics2D object. (make sure you disable double buffering for the JComponent while printing). Some objects (JTextComponents and JTables can do this all for you but you have to implement the above yourself for other JComponents).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 1
    Last Post: 07-25-2011, 11:24 AM
  2. Replies: 4
    Last Post: 01-10-2010, 08:48 AM
  3. change url in parent window from child window
    By rakesh_n_mehta in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-09-2009, 01:17 PM
  4. [SOLVED] printing address of current instance of object?
    By emceenugget in forum New To Java
    Replies: 1
    Last Post: 02-09-2009, 10:36 PM
  5. Replies: 8
    Last Post: 12-01-2008, 03:07 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •