Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8665077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:23:46+00:00 2026-06-12T17:23:46+00:00

I have been working in a big program and one of its functionalities should

  • 0

I have been working in a big program and one of its functionalities should be to print the contents of the main window. I checked the API and found this example:

http://docs.oracle.com/javase/tutorial/2d/printing/gui.html

it was very helpful, I tried to use that code in my program by placing this inside the actionperformed method of my print button:

PrinterJob job = PrinterJob.getPrinterJob();
     job.setPrintable(this);
     boolean ok = job.printDialog();
     if (ok) {
         try {

             job.print();
         } catch (PrinterException ex) {
          /* The job did not successfully complete */
         }
     }

If I click the print button, I get a printer dialog and when I tell it to print, it just prints a blank document. I know the above code is not all I need, as I’ve seen in the API’s examples there is a print() method, but apparently they never call it, so it is pretty confusing. I’ve tried calling and using it many times, but with no success.

Also, I think that when I finally get it to print, my window will need to be printed in the landscape orientation, it even may need some scaling. Any ideas on how to do that?

I would like any useful help to help me implement this code successfully. I know I should be able to do it by myself just by checking the documentation (I’ve tried for almost 2 days now) but I can’t get it to work. I’ve learned all the programming I know through the internet. Any help will be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:23:47+00:00Added an answer on June 12, 2026 at 5:23 pm

    Here’s a simple solution. I have a Printer class that implements printable and it will handle the printing job:

    public static class Printer implements Printable {
        final Component comp;
    
        public Printer(Component comp){
            this.comp = comp;
        }
    
        @Override
        public int print(Graphics g, PageFormat format, int page_index) 
                throws PrinterException {
            if (page_index > 0) {
                return Printable.NO_SUCH_PAGE;
            }
    
            // get the bounds of the component
            Dimension dim = comp.getSize();
            double cHeight = dim.getHeight();
            double cWidth = dim.getWidth();
    
            // get the bounds of the printable area
            double pHeight = format.getImageableHeight();
            double pWidth = format.getImageableWidth();
    
            double pXStart = format.getImageableX();
            double pYStart = format.getImageableY();
    
            double xRatio = pWidth / cWidth;
            double yRatio = pHeight / cHeight;
    
    
            Graphics2D g2 = (Graphics2D) g;
            g2.translate(pXStart, pYStart);
            g2.scale(xRatio, yRatio);
            comp.paint(g2);
    
            return Printable.PAGE_EXISTS;
        }
    }
    

    Next, to print it:

    JFrame yourComponent = new JFrame();
    PrinterJob pjob = PrinterJob.getPrinterJob();
    PageFormat preformat = pjob.defaultPage();
    preformat.setOrientation(PageFormat.LANDSCAPE);
    PageFormat postformat = pjob.pageDialog(preformat);
    //If user does not hit cancel then print.
    if (preformat != postformat) {
        //Set print component
        pjob.setPrintable(new Printer(yourComponent), postformat);
        if (pjob.printDialog()) {
            pjob.print();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am refurbishing a big CMS that I have been working on for quite
I have been working with SQL Server as a Developer a while. One thing
I have been working on a site that makes some pretty big use of
I have been working on a class and it is all big and ugly
Have been working on this question for a couple hours and have come close
I have been working on a large java application. It is quite parallel, and
I have been working on this app for at least 3-4 months and just
I have been working on a project in C# (.net4). Project pretty much allows
I have been working on a project on and off, but I haven't touched
I have been working with Zend for a few months now and am at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.