Printing a JPanel

Discussion in 'Java' started by Justin, Jan 15, 2007.

  1. Justin

    Justin Guest

    I have a class that extends a JPanel and implements Printable. The
    class has several JLabel and a JTextArea. I am trying print the JPanel
    using the default printer.

    I believe the code below is the relevant portion:

    public int print(Graphics g, PageFormat pageFormat, int pageIndex)
    throws PrinterException {
    if (pageIndex >= 1) {
    return Printable.NO_SUCH_PAGE;
    }

    Graphics2D g2D = (Graphics2D) g;
    g2D.translate(100, 200);
    paintComponents(g2D);
    // successful printing of the page
    return Printable.PAGE_EXISTS;
    }

    And the area around the ActionEvent that triggers the print method is:

    if (ae.getSource() == btnPrint){

    printJob = PrinterJob.getPrinterJob();
    printJob.setPrintable(panel);

    boolean ok = printJob.printDialog();
    if (ok) {
    try {
    printJob.print();
    } catch (Exception pe) {
    System.out.println("Printing Exception Occured!");
    pe.printStackTrace();
    }
    }
    }


    When I press the print button, the print method works, but the document
    is blank (I am using MS Office Document Image Writer, so the 'Printer'
    is not out of ink).

    As always, any help is appreciated, and if I havent supplied enough
    code, let me know.

    Thanks

    Justin
     
    Justin, Jan 15, 2007
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon B
    Replies:
    7
    Views:
    7,998
    =?Utf-8?B?SmFzb25DaG9p?=
    Jan 30, 2006
  2. Andreas Beresko

    Add a JPanel to a JPanel...

    Andreas Beresko, Jul 14, 2004, in forum: Java
    Replies:
    6
    Views:
    27,563
  3. fredjeboy
    Replies:
    0
    Views:
    1,487
    fredjeboy
    Mar 13, 2005
  4. JustSomeGuy
    Replies:
    13
    Views:
    711
    msalters
    Dec 9, 2004
  5. Raphael Jolivet

    [JPanel] Drop Down JPanel

    Raphael Jolivet, Jul 18, 2008, in forum: Java
    Replies:
    1
    Views:
    2,014
    John B. Matthews
    Jul 18, 2008
  6. t1m1976
    Replies:
    1
    Views:
    2,755
    t1m1976
    Nov 7, 2010
  7. iffy agbim
    Replies:
    1
    Views:
    239
    Mark Andrews
    May 24, 2004
  8. iffy agbim
    Replies:
    0
    Views:
    344
    iffy agbim
    May 21, 2004
Loading...