I am trying to create a buffered image from a table, when i add the table to a application frame and set the size I am able to view it properly but when I convert it into an
Basically, you have to do the layout work for all components you want to paint to the image, that is in this case both the table and the tableHeader, by setting their sizes (below the pref is used).
BTW, adding to a JScrollPane is not helpful (as you have seen), except when the pane is realized - adding the header is done by the table in addNotify.
JTable table = new JTable(new AncientSwingTeam());
JTableHeader header =table.getTableHeader();
table.setSize(table.getPreferredSize());
header.setSize(header.getPreferredSize());
int w = Math.max(table.getWidth(), header.getWidth());
int h = table.getHeight() + header.getHeight();
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
header.paint(g2);
g2.translate(0, header.getHeight());
table.paint(g2);
g2.dispose();
JLabel label = new JLabel(new ImageIcon(bi));
showInFrame(label, "image of table");
Edit: comment on TextAreaRenderer
The general rule is to never-ever-ever change the calling table in the renderer's getXXRendererComponent, the table given as parameter is to be regarded read-only, strictly. Breaking the rule can lead to ugly loops (setting a new rowHeight while painting triggers a new painting request) or artefacts (there is no guarantee when the renderer is called, so the correct rowheight might or not have been set) as you see here.
Instead do the measuring somewhere outside. On detecting a change (f.i. in the model) which might lead to updating the sizes, walk the rows, measure all its cells and update to the largest. Simply extract all your sizing code from the renderer.