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

    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);

    JMenu mnFile = new JMenu("File");

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

    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();
    if (printJob.printDialog())
    try {
    } catch(PrinterException pe) {
    System.out.println("Error printing: " + pe);

    public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
    if (pageIndex > 0) {
    } else {
    Graphics2D g2d = (Graphics2D)g;
    g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

    /** 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);

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

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

    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.

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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power


    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,

    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 1
    Last Post: 07-25-2011, 10:24 AM
  2. Replies: 4
    Last Post: 01-10-2010, 07: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, 12: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, 09:36 PM
  5. Replies: 8
    Last Post: 12-01-2008, 02: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