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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:28:07+00:00 2026-05-23T02:28:07+00:00

in relation to this thread I have a question if someone to know if

  • 0

in relation to this thread I have a question if someone to know if is possible to override/change larger font (Font Type, Size, Color) for MessageFormat headerFormat comings with JTable.PrintMode or I must paint g2.drawString(“my header/footer”) and JTable#print() separatelly

  • 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-23T02:28:08+00:00Added an answer on May 23, 2026 at 2:28 am

    As everybody already mentioned (while I was relaxing in vacation 🙂 – TablePrintable is tightly knitted for secrecy, no way to subclass, no way to configure the header/footer printing. The only option to hook is to wrap the table’s default printable, let it do its work without header/footer and take over the header/footer printing oneself.

    The problem with the snippets shown so far is that they dont play nicely with multi-page – as known and mentioned by all authors, of course – because the default printable thinks there are no headers/footers and freely uses the space required by them. Not surprisingly 🙂

    So the question is: is there a way to make to default not print into the region of the header/footer? And yeah, it is: double-wopper (ehh .. wrapper) is the answer – make the default printable believe it has less printable space by wrapping the given pageFormat into one that returns a adjusted getImageableHeight/Y. Something like:

    public class CustomPageFormat extends PageFormat {
    
        private PageFormat delegate;
        private double headerHeight;
        private double footerHeight;
    
        public CustomPageFormat(PageFormat format, double headerHeight, double footerHeight) {
            this.delegate = format;
            this.headerHeight = headerHeight;
            this.footerHeight = footerHeight;
        }
        /** 
         * @inherited <p>
         */
        @Override
        public double getImageableY() {
            return delegate.getImageableY() + headerHeight;
        }
    
        /** 
         * @inherited <p>
         */
        @Override
        public double getImageableHeight() {
            return delegate.getImageableHeight() - headerHeight - footerHeight;
        }
    
        // all other methods simply delegate
    

    Then use in the printable wrapper (footer has to be done similarly):

    public class CustomTablePrintable implements Printable {
    
        Printable tablePrintable;
        JTable table;
        MessageFormat header; 
        MessageFormat footer;
    
        public CustomTablePrintable(MessageFormat header, MessageFormat footer) {
            this.header = header;
            this.footer = footer;
        }
    
        public void setTablePrintable(JTable table, Printable printable) {
            tablePrintable = printable;        
            this.table = table;
        }
    
        @Override
        public int print(Graphics graphics, PageFormat pageFormat, 
                int pageIndex) throws PrinterException {
            // grab an untainted graphics
            Graphics2D g2d = (Graphics2D)graphics.create();
            // calculate the offsets and wrap the pageFormat
            double headerOffset = calculateHeaderHeight(g2d, pageIndex);
            CustomPageFormat wrappingPageFormat = new CustomPageFormat(pageFormat, headerOffset, 0);
            // feed the wrapped pageFormat into the default printable
            int exists = tablePrintable.print(graphics, wrappingPageFormat, pageIndex);
            if (exists != PAGE_EXISTS) {
                g2d.dispose();
                return exists;
            }
            // translate the graphics to the start of the original pageFormat and draw header
            g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
            printHeader(g2d, pageIndex, (int) pageFormat.getImageableWidth());
            g2d.dispose();
    
            return PAGE_EXISTS;        
        }
    
    
        protected double calculateHeaderHeight(Graphics2D g, int pageIndex) {
            if (header == null) return 0;
            Object[] pageNumber = new Object[]{new Integer(pageIndex + 1)};
            String text = header.format(pageNumber);
            Font headerFont = table.getFont().deriveFont(Font.BOLD, 18f);
            g.setFont(headerFont);
            Rectangle2D rect = g.getFontMetrics().getStringBounds(text, g);
            return rect.getHeight();
        }
    
        protected void printHeader(Graphics2D g, int pageIndex, int imgWidth) {
            Object[] pageNumber = new Object[]{new Integer(pageIndex + 1)};
            String text = header.format(pageNumber);
            Font headerFont = table.getFont().deriveFont(Font.BOLD, 18f);
            g.setFont(headerFont);
            Rectangle2D rect = g.getFontMetrics().getStringBounds(text, g);
            // following is c&p from TablePrintable printText
            int tx;
    
            // if the text is small enough to fit, center it
            if (rect.getWidth() < imgWidth) {
                tx = (int) ((imgWidth - rect.getWidth()) / 2);
    
                // otherwise, if the table is LTR, ensure the left side of
                // the text shows; the right can be clipped
            } else if (table.getComponentOrientation().isLeftToRight()) {
                tx = 0;
    
                // otherwise, ensure the right side of the text shows
            } else {
                tx = -(int) (Math.ceil(rect.getWidth()) - imgWidth);
            }
    
            int ty = (int) Math.ceil(Math.abs(rect.getY()));
            g.setColor(Color.BLACK);
            g.drawString(text, tx, ty);
    
        }
    }
    

    And at the end return that from table’s getPrintable, like:

        final JTable table = new JTable(myModel){
    
            /** 
             * @inherited <p>
             */
            @Override
            public Printable getPrintable(PrintMode printMode,
                    MessageFormat headerFormat, MessageFormat footerFormat) {
                Printable printable = super.getPrintable(printMode, null, null);
                CustomTablePrintable custom = new CustomTablePrintable(headerFormat, footerFormat);
                custom.setTablePrintable(this, printable);
                return custom;
            }
    
        };
    

    printHeader/Footer can be implemented to do whatever is required.

    At the end of the day: the answer to the question “do I need to call g.drawString(…)” still is “Yes”. But at least it’s safely outside of the table itself 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In relation to this question ( Efficient hashCode() implementation ) I have one more
I have this mapper defined: mapper(Resource, resource_table, properties = {'type' : relation(ResourceType,lazy = False),
This question is based on the thread . If we have one-to-many data structure,
In relation to this question on Using OpenGL extensions , what's the purpose of
This is in relation to this question I am hosting this WCF service in
In relation to this stackoverflow question , how would I go about creating my
This is in relation to this question . The proposed answers involve adding a
I have this problem decomposing a relation schema into a set of schemas that
I have a relation mapping table like this: attributeid bigint productid bigint To clean
This is in relation to this other SO question which asks how to overwrite

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.