Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / April 2004

Tip: Looking for answers? Try searching our database.

how to convert a JTable to an image?

Thread view: 
Ben Wilson - 20 Apr 2004 06:34 GMT
Hi
I have code that will convert a JTable to an image, but the problem is that
if the height of the JTable is greater than the height of the screen, the
bottom of the table is cut off. I do not want to display the JTable on the
screen at any time. Here is my code:

myJTable.setSize(width, height);
JScrollPane scrollPane = new JScrollPane(myJTable);
scrollPane.setBorder(new javax.swing.border.EmptyBorder(2, 2, 2, 2));
scrollPane.setViewportBorder(new javax.swing.border.EmptyBorder(0,0,0,0));
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
scrollPane.setPreferredSize(new Dimension(width, height));
JFrame frame = new JFrame();
frame.getContentPane().add(scrollPane);
frame.pack();
frame.setSize(width, height);
BufferedImage buffered = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = buffered.createGraphics();
scrollPane.printAll(g2);
g2.dispose();
frame.getContentPane().removeAll();
frame.dispose();
return buffered;

If anyone knows how to do this in a way that is not dependent on the
resolution of the screen, please let me know.

Thanks!
Ben
ak - 20 Apr 2004 07:09 GMT
> I have code that will convert a JTable to an image, but the problem is that
> if the height of the JTable is greater than the height of the screen, the
> bottom of the table is cut off. I do not want to display the JTable on the
> screen at any time. Here is my code:

you can place your frame left from screen, so it is invisible for user.

> myJTable.setSize(width, height);
> JScrollPane scrollPane = new JScrollPane(myJTable);
> scrollPane.setBorder(new javax.swing.border.EmptyBorder(2, 2, 2, 2));
> scrollPane.setViewportBorder(new javax.swing.border.EmptyBorder(0,0,0,0));

scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
> scrollPane.setPreferredSize(new Dimension(width, height));
> JFrame frame = new JFrame();
[quoted text clipped - 5 lines]
> Graphics2D g2 = buffered.createGraphics();
> scrollPane.printAll(g2);

why scrollPane.printAll(g2) ?
you should ask JTable for preferredSize and create BufferedImage with
appropriate size.
then just paint myJTable into g2.

--

____________

http://reader.imagero.com the best java image reader.
Ben Wilson - 20 Apr 2004 17:39 GMT
ak
Thanks for responding...

> > I have code that will convert a JTable to an image, but the problem is
>  that
[quoted text clipped - 3 lines]
>
> you can place your frame left from screen, so it is invisible for user.

I have already achieved the process of creating an image of the table
without showing it on the screen. The only problem that remains is
that the table cannot be bigger than the screen, and if it is, it gets
cut off. I don't know why the table has to be associated with the
screen at all since I don't want to display it.

> > myJTable.setSize(width, height);
> > JScrollPane scrollPane = new JScrollPane(myJTable);
[quoted text clipped - 16 lines]
> appropriate size.
> then just paint myJTable into g2.

You suggestion works too, however it doesn't address the problem. It
appears that a frame cannot be bigger than the screen, and a component
cannot be laid out unless it is in a frame. If anyone knows a way
around this, I would really like to know!

Thanks
Ben
Thomas Weidenfeller - 21 Apr 2004 07:59 GMT
> You suggestion works too, however it doesn't address the problem. It
> appears that a frame cannot be bigger than the screen, and a component
> cannot be laid out unless it is in a frame. If anyone knows a way
> around this, I would really like to know!

AffineTransform, ScrollPane, and maybe a few more.

/Thomas


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.