Results 1 to 5 of 5

Thread: Print Jpanel and its jcomponents

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Posts
    2

    Unhappy Print Jpanel and its jcomponents

    Hi im having problems trying to print a jpanel and all the jcomponents inside it(jlabels, jtextarea etc). Im having problems trying to print it in landscape format. the width seems to be cut halfway when trying to print it in landscape. Works fine in portrait though.

    Apparently all settings changed in the page and printer dialog that pops up doesnt seem to work for the page that im trying to print too.

    Code:
    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);
        PageFormat pageFormat = printJob.pageDialog(printJob.defaultPage());
        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;
          pageFormat.setOrientation(PageFormat.LANDSCAPE);
          g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
    disableDoubleBuffering(componentToBePrinted);
          componentToBePrinted.paint(g2d);
          enableDoubleBuffering(componentToBePrinted);
          return(PAGE_EXISTS);
    
        }
    The above is close to the entire class which i use for printing out the jpanel. Its not my work im just trying to modify it to work for me.(credits to Marty Hall, http://www.apl.jhu.edu/~hall/java/).

    The code that is used to print the jpanel is
    PrintUtilities.printComponent(jPanel);

    Please help thanks in advance!

  2. #2

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Posts
    2

    Unhappy

    Sorry accidentally doubleposted. Tried to delete but had an access denied error. Sorry please reply in the other thread with the same name and if an admin sees this please delete this thread.

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Your other thread has been deleted. You should have been able to delete it. Next time just click the edit button then check the delete box and hit Delete Now.

  4. #4
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    we can't delete now mate Dilenger4, the thread in general. but we can delete a post though. i don't know why. visuaAd told me about a week ago. well of course you can, you're a mod.

  5. #5

Posting Permissions

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



Click Here to Expand Forum to Full Width