• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to save jtable as image

 
Cathy Song
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

I have created a jTable using TableModel which extends AbstractTableModel.
Now I am trying to save this table as an image (.jpg/.png/.gif)

How should I approach this problem?

Thanks,
Cathy.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a JGuru article that shows you how to do it. Pretty simple really after I read that.
 
Cathy Song
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Gregg.

--Cathy.

[ July 12, 2004: Message edited by: Cathy Song ]
[ July 12, 2004: Message edited by: Cathy Song ]
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Cathy Song:
Thanks Gregg. Also is com.sun.image.codec.jpeg part of the Java Advanced Imaging JRE?

--Cathy.

[ July 12, 2004: Message edited by: Cathy Song ]


Nope, just the standard JDK.
 
Cathy Song
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void saveComponentAsJPEG(JTable table, String filename) {
java.awt.Image image = table.createImage(500, 500);

BufferedImage myImage =
new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);

Graphics2D g2 = myImage.createGraphics();
table.paint(g2);

try {
java.io.OutputStream out = new FileOutputStream(filename);
com.sun.image.codec.jpeg.JPEGImageEncoder encoder =
com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(out);
encoder.encode(myImage);
out.close();
}
catch (Exception e) {
System.out.println(e);
}
}

This method is creating a .jpg file, but nothing in it (with a black background) !!

Could anyone tell me whats going on here?
Thanks,
Cathy.
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the JTable being displayed visually? If not it may be set to a default size of 0 pixels by 0 pixels, which doesn't make for a very interesting picture unless you call setSize(width, height) manually.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For future implementations I would suggest getting the H and W of the component versus specifiying it in the method. This may eleviate some of those kinds of issues if it is a size/display problem.
 
Cathy Song
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks All.

--> table.setSize(width, ht) did the trick. Now I am working on the issue of the disappearing column header. These are not included in the image. I wonder why.

Thanks again,
Cathy.
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is probably layout issues again, since JTableHeader is it's own component which may initially have a size 0 by 0 that never gets changed. Perhaps calling table.doLayout() first will work?
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's probably better to use table.validate() than doLayout().
 
Cathy Song
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since JTableHeader is it's own component, I am currently just creating 2 seperate images one with the header and one with the table contents. Still experimenting with the initial problem, will post the answer if I am successful.

Thanks a lot for your help.

Cathy.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you put the JTable in a JScrollPane and save the JScrollPane as an Image, it should save everything in the scroll pane, including the headers.

If you need ALL the data displayed then just put the JTable on it's own JPanel and save the JPanel which should give the same results.
[ July 12, 2004: Message edited by: Gregg Bolinger ]
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ImageIO class is new in j2se 1.4.
 
Cathy Song
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Craig. The sample code you provided works great.

-Cathy.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!