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 794729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:21:39+00:00 2026-05-14T22:21:39+00:00

I’m trying to do a printable component (an invoice document). I use JComponent instead

  • 0

I’m trying to do a printable component (an invoice document). I use JComponent instead of JPanel because I don’t want a background. The component has many subcomponents.

The main component implements Printable and has a print-method that is calling printAll(g) so that all subcomponents should be printed. But my subcomponents doesn’t print.

What am I missing? Does all subcomponents also has to implement Printable?

In my code below, the TopHeader is not printed.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class PPanel extends JComponent implements Printable {
    static double w;
    static double h;

    public PPanel() {
        this.setLayout(new BorderLayout());

        this.add(new JLabel("Document Body"), BorderLayout.CENTER);
        this.add(new TopHeader(), BorderLayout.NORTH);
    }

    class TopHeader extends JComponent {
        public TopHeader() {
            this.setLayout(new BorderLayout());
            JLabel companyName = new JLabel("Company name");
            JLabel docType = new JLabel("Document type");
            this.add(companyName, BorderLayout.WEST);
            this.add(docType, BorderLayout.EAST);
        }
    }

    public static void main(String[] args) {
        final PPanel p = new PPanel();
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(p);
        try {
            job.print();
        } catch (PrinterException ex) {
            // print failed
        }
            // Preview
        new JFrame() {{ getContentPane().add(p); this.setSize((int)w, (int)h); setVisible(true); }};

    }

    @Override
    public int print(Graphics g, PageFormat pf, int page)
            throws PrinterException {
        if (page > 0) {
            return NO_SUCH_PAGE;
        }

        Graphics2D g2d = (Graphics2D)g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());

        w = pf.getImageableWidth();
        h = pf.getHeight();

        this.setSize((int)w, (int)h);
        this.setPreferredSize(new Dimension((int)w, (int)h));
        this.doLayout();

        this.printAll(g);
        return PAGE_EXISTS;
    }
}
  • 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-05-14T22:21:39+00:00Added an answer on May 14, 2026 at 10:21 pm

    You could probably loop through the Component[] returned by getComponents(), but a simple expedient is shown below. Note the difference between the screen preview and the printout. Also, note the use of validate() in preference to doLayout(). Finally, as a convenience for previewing, this example implements Using Print Setup Dialogs.

    Addendum: You might also look at Printing Support in Swing Components and compare the approach taken in this example.

    import java.awt.*;
    import java.awt.print.*;
    import javax.swing.*;
    
    public class PPanel extends JComponent implements Printable {
    
        private JComponent top = new TopHeader();
        private JComponent mid = new JLabel("Document Body");
    
        public PPanel() {
            this.setLayout(new BorderLayout());
            this.add(top, BorderLayout.NORTH);
            this.add(mid, BorderLayout.CENTER);
        }
    
        private static class TopHeader extends JComponent {
    
            public TopHeader() {
                this.setLayout(new BorderLayout());
                JLabel companyName = new JLabel("Company name");
                JLabel docType = new JLabel("Document type");
                this.add(companyName, BorderLayout.WEST);
                this.add(docType, BorderLayout.EAST);
            }
        }
    
        @Override
        public int print(Graphics g, PageFormat pf, int page)
            throws PrinterException {
            if (page > 0) {
                return NO_SUCH_PAGE;
            }
            Graphics2D g2d = (Graphics2D)g;
            g2d.translate(pf.getImageableX(), pf.getImageableY());
            int w = (int)pf.getImageableWidth();
            int h = (int)pf.getImageableHeight();
            top.setSize(new Dimension(w, top.getPreferredSize().height));
            this.setSize(w, h);
            this.validate();
            this.printAll(g2d);
            return PAGE_EXISTS;
        }
    
        public static void main(String[] args) {
            final PPanel p = new PPanel();
            // Preview before print()
            new JFrame() {
                {
                    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    this.add(p);
                    this.pack();
                    this.setVisible(true);
                }
            };
            PrinterJob pj = PrinterJob.getPrinterJob();
            PageFormat pf = pj.pageDialog(pj.defaultPage());
            pj.setPrintable(p, pf);
            if (pj.printDialog()) {
                try {
                    pj.print();
                } catch (PrinterException pe) {
                    pe.printStackTrace();
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 474k
  • Answers 474k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think you should do something similar to dropbox app.… May 16, 2026 at 4:09 am
  • Editorial Team
    Editorial Team added an answer If by null you mean the empty string, you want:… May 16, 2026 at 4:09 am
  • Editorial Team
    Editorial Team added an answer Try: SELECT * FROM groupmembers g INNER JOIN users u… May 16, 2026 at 4:09 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.