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

  • Home
  • SEARCH
  • 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 6553517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:35:28+00:00 2026-05-25T12:35:28+00:00

I was offering advice on capturing an image of tabular data on Java API

  • 0

I was offering advice on capturing an image of tabular data on Java API or Tool to convert tabular data into PNG image file – when the OP requested a code sample. Turns out to be harder than I thought! The JTable header vanishes from the PNG that the code writes.

PNG

PNG

Screen shot

enter image description here

import javax.swing.*;
import java.awt.Graphics;
import java.awt.BorderLayout;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import java.io.File;

class TableImage {

    public static void main(String[] args) throws Exception {
        Object[][] data = {
            {"Hari", new Integer(23), new Double(78.23), new Boolean(true)},
            {"James", new Integer(23), new Double(47.64), new Boolean(false)},
            {"Sally", new Integer(22), new Double(84.81), new Boolean(true)}
        };

        String[] columns = {"Name", "Age", "GPA", "Pass"};

        JTable table = new JTable(data, columns);
        JScrollPane scroll = new JScrollPane(table);
        JPanel p = new JPanel(new BorderLayout());
        p.add(scroll,BorderLayout.CENTER);

        JOptionPane.showMessageDialog(null, p);

        BufferedImage bi = new BufferedImage(
            (int)p.getSize().getWidth(),
            (int)p.getSize().getHeight(),
            BufferedImage.TYPE_INT_RGB
            );

        Graphics g = bi.createGraphics();
        p.paint(g);

        JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi)));
        ImageIO.write(bi,"png",new File("table.png"));
    }
}

Note: I checked over camickr’s Screen Image class and included a call to the doLayout(Component) method. The method is useful for if a Component has never been realized on screen, but has no effect on this code (which pops the panel containing the table in an option pane before trying to render it).

What is needed in order to get the table header to render?

Update 1

Changing the line..

        p.paint(g);

..to (with an appropriate import)..

        p.paint(g);
        JTableHeader h = table.getTableHeader();
        h.paint(g);

..produces..

2nd try

I’ll keep tweaking it.

Update 2

kleopatra (strategy 1) & camickr (strategy 2) have provided an answer each, both of which work, & neither of which requires adding the JTable to a dummy component (which is an huge hack IMO).

While strategy 2 will crop (or expand) to ‘just the table’, the 1st strategy will capture the panel containing the table. This becomes problematic if the table contains many entries, showing an image of a truncated table with a scroll bar.

While strategy 1 might be further tweaked to get around that, I really like the neat simplicity of strategy 2, so it gets the tick.

As pointed out by kleopatra, there was no ‘tweak’ needed. So I’ll try again..

Update 3

This is the image produced by the methods put forward by both camickr and kleopatra. I’d have put it twice, but to my eye, they are identical (though I have not done a pixel by pixel comparison).

JTable in Nimbus PLAF

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import java.io.File;

class TableImage {

    String[] columns = {"Name", "Age", "GPA", "Pass"};
    /** Any resemblance to persons living or dead is purely incidental. */
    Object[][] data = {
        {"André", new Integer(23), new Double(47.64), new Boolean(false)},
        {"Jeanie", new Integer(23), new Double(84.81), new Boolean(true)},
        {"Roberto", new Integer(22), new Double(78.23), new Boolean(true)}
    };

    TableImage() {
    }

    public JTable getTable() {
        JTable table = new JTable(data, columns);
        table.setGridColor(new Color(115,52,158));
        table.setRowMargin(5);
        table.setShowGrid(true);

        return table;
    }

    /** Method courtesy of camickr.
    https://stackoverflow.com/questions/7369814/why-does-the-jtable-header-not-appear-in-the-image/7375655#7375655
    Requires ScreenImage class available from..
    http://tips4java.wordpress.com/2008/10/13/screen-image/ */
    public BufferedImage getImage1(JTable table) {
        JScrollPane scroll = new JScrollPane(table);

        scroll.setColumnHeaderView(table.getTableHeader());
        table.setPreferredScrollableViewportSize(table.getPreferredSize());

        JPanel p = new JPanel(new BorderLayout());
        p.add(scroll, BorderLayout.CENTER);

        BufferedImage bi = ScreenImage.createImage(p);
        return bi;
    }

    /** Method courtesy of kleopatra.
    https://stackoverflow.com/questions/7369814/why-does-the-jtable-header-not-appear-in-the-image/7372045#7372045 */
    public BufferedImage getImage2(JTable table) {
        JScrollPane scroll = new JScrollPane(table);

        table.setPreferredScrollableViewportSize(table.getPreferredSize());

        JPanel p = new JPanel(new BorderLayout());
        p.add(scroll, BorderLayout.CENTER);

        // without having been shown, fake a all-ready
        p.addNotify();

        // manually size to pref
        p.setSize(p.getPreferredSize());

        // validate to force recursive doLayout of children
        p.validate();

        BufferedImage bi = new BufferedImage(p.getWidth(), p.getHeight(), BufferedImage.TYPE_INT_RGB);

        Graphics g = bi.createGraphics();
        p.paint(g);
        g.dispose();

        return bi;
    }

    public void writeImage(BufferedImage image, String name) throws Exception {
        ImageIO.write(image,"png",new File(name + ".png"));
    }

    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        TableImage ti = new TableImage();
        JTable table;
        BufferedImage bi;

        table = ti.getTable();
        bi = ti.getImage1(table);
        ti.writeImage(bi, "1");
        JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi)));

        table = ti.getTable();
        bi = ti.getImage2(table);
        ti.writeImage(bi, "2");
        JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi)));
    }
}

Both achieve the goal. Using camickr’s method you leverage the further power of the ScreenImage API. Using kleopatra’s method – about a dozen lines (less the comments and white space) of pure J2SE.

While ScreenImage is a class I will use and recommend in future, the other approach using core J2SE is what I’d probably use for this exact circumstance.

So while the ‘tick’ will stay with camickr, the bounty is going to kleopatra.

  • 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-25T12:35:29+00:00Added an answer on May 25, 2026 at 12:35 pm

    It was not a requirement of this thread to render the table without first displaying it, but that was the ultimate goal

    ScreenImage handles this.

    You must manually add the header to the scrollpane.

    import javax.swing.*;
    import java.awt.Graphics;
    import java.awt.BorderLayout;
    import java.awt.image.BufferedImage;
    
    import javax.imageio.ImageIO;
    import java.io.File;
    
    class TableImage {
    
        public static void main(String[] args) throws Exception {
            Object[][] data = {
                {"Hari", new Integer(23), new Double(78.23), new Boolean(true)},
                {"James", new Integer(23), new Double(47.64), new Boolean(false)},
                {"Sally", new Integer(22), new Double(84.81), new Boolean(true)}
            };
    
            String[] columns = {"Name", "Age", "GPA", "Pass"};
    
            JTable table = new JTable(data, columns);
            JScrollPane scroll = new JScrollPane(table);
    
            scroll.setColumnHeaderView(table.getTableHeader());
            table.setPreferredScrollableViewportSize(table.getPreferredSize());
    
            JPanel p = new JPanel(new BorderLayout());
            p.add(scroll, BorderLayout.CENTER);
    
            BufferedImage bi = ScreenImage.createImage(p);
    
            JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi)));
            ImageIO.write(bi,"png",new File("table.png"));
        }
    }
    

    Note: Kleopatra’s suggeston to use the addNotify() on the panel will not work with ScreenImage. The addNotify() method makes the component displayable and the ScreenImage code will only lay out the components for non-displayable components. I might look into making this more general.

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

Sidebar

Related Questions

I am looking for a WorkQueue API offering the following features: java.util.Queue compatible offers
Does anybody know a provider offering TV listings (through API or download) for all
what tool can I use to find classes present in a C++ static library
I have scoured the internet looking for a solution to this and I am
I am torn between Wicket and Vaadin. I am starting a micro-isv and need
On this website: when the user clicks register now along the top bar the
Hey I'm new to PHP so would really like your insight on how I'm
i have a pretty map of the US: http://upload.wikimedia.org/wikipedia/commons/a/a5/Map_of_USA_with_state_names.svg i would like to implement
I have a web application that has a WebDav share for Word that uses
I am moving a C++ project from Windows to Linux and I now need

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.