• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to create a jpg image from a JTable ?

 
satishp patil
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to create a jpg image from a JTable ?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch. The gist is something like this:
 
satishp patil
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to create and save this image to the clipboard so that I can paste this to the document. How do I do it ?
 
satishp patil
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its more complicated than what I need.
I have a JTable displayed in the application and on the right click of it I have a option to copy it into the clipboard as an image so that it can be pasted into the .doc file.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its more complicated than what I need.

That's too bad, because it does't get simpler than that.
 
satishp patil
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a Jtable and I want to create image of it and save it to clipboard.
But before creating an image I want to check the estimated image size in bytes from the tables length and width(which is in int).
Please help...
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's impossible to say beforehand, due to the algorithms used by JPEG. Why does that matter?

If it turns out to be too large for your purposes, you can reduce image quality (and thus size) using the ImageIO package.
 
satishp patil
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want something like :

private long getEstimatedImageMemory(int tableWidth, int tableHeight) {
return ****;
}

I want to check before copying image to clipboard like the application have sufficient memory for running other task simultaneously.
So for this I do not want to create image directly and then check its size .I want to check for the estimated size from the table length and width before and then create and copy image to clipboard
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like I said, not possible. If this was my problem, I'd implement the feature, and then see if it turns out to be an actual problem (as opposed to an imagined one). Screenshots do not tend to be large, and since this is a desktop app it should not be a problem to start the JVM with an extra 64MB of memory if that turns out to be necessary.
 
satishp patil
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The issue is that I don't want them to be created on the disk and then check out there size.
private BufferedImage createImage(int totalWidth, int totalHeight, JTable table, JTableHeader header) {
BufferedImage image = new BufferedImage(totalWidth, totalHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
header.paint(g2);
g2.translate(0, header.getPreferredSize().height);
table.paint(g2);
g2.dispose();
return image;
}

after this I want the size of this image in bytes/mb without writing it to the disk.
it can be done?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not creating a disk file is a different question than not creating a JPEG. ImageIO.write can write to an OutputStream instead of a File (see its javadocs), so you can use a ByteArrayOutputStream from which you can get the size.
 
satishp patil
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ho to convert an int value into MB ?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please drop the habit of quoting entire posts. Quoting is for extracting specific bits of a previous posts that your reply references. Otherwise it makes no sense, and you should use the "Post reply" button instead.

The size of the byte[] would be the image size in bytes. How do you convert bytes to megabytes?
 
satishp patil
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"I have an integer value say int i = 10;
Now I want to convert this int to mb.This is the requirement which I have."
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please reread my previous post about how to quote properly, and act accordingly in the future.

It doesn't make sense to "convert an int to a MB". If you have a number that indicates a measurement taken in bytes, then you can convert that to kilobytes or megabytes; is that what you are asking? If so, you need an understanding what a MB is, and how it relates to bytes; Wikipedia can teach you all about that. Once you know how bytes, kilobytes, megabytes etc. are related, the conversions should be straightforward.

If you are asking something else, please explain in more detail what it is, and how it is related to your earlier questions about the size of images.
 
satishp patil
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have JTable and from his length and width(both are int) I want to check how much memory will be required for the image created out of it.
This is the requirement which cant be changed.So unless the required memory in bytes calculated I can move on further.
May be this looks unusual but this is the final requirement.
 
satishp patil
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following is the requirement:

The amount of memory that is used by the image is estimated by the use of the following formula
memory amount = length * width * 4 bytes
where the length and the width are the full sizes of the table in bits.
 
satishp patil
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have table length and width in int available.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the final requirement.

An important skill of a software developer is to realize when given requirements can't be satisfied, or when they would take much more work to implement than something that is similar, but not quite the same. You'll be surprised how often whoever came up with the requirements will be flexible about them when facing expert analysis to this effect.

I'm still assuming that you're worrying about something being a problem when it actually isn't. Given how simple it is to implement what I suggested, there is really no reason not to try it and see if it is an actual problem (as opposed to an assumed problem). What's more, it'd give you some hard data points for estimating the image size - which is as good as it'll get, given what I said earlier about the impossibility of knowing how big a JPEG will be.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
satishp patil wrote:The amount of memory that is used by the image is estimated by the use of the following formula
memory amount = length * width * 4 bytes
where the length and the width are the full sizes of the table in bits.

Well, this formula would make some sense for a size of a typical uncompressed image with the width and height specified in pixels.

Are you sure you fully understand the requirements you were given? (Not understanding them is not necessarily your fault, it's not uncommon for specifications to be incomplete, confusing or outright wrong, especially when given by people who aren't developers.)
 
satishp patil
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply, but so far the document which I have specify the same requirement and there is no change in this requirement. So I am still looking for the solution based on the above formula.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
satishp patil wrote:the document which I have specify the same requirement and there is no change in this requirement.

And I suspect there won't be, unless and until you take steps to make that happen. While I can imagine several reasons why you're reluctant to do that, this reluctance doesn't help you, because a) the exact requirement can't be satisfied, and b) it seems details of the implementation were somehow allowed to be part of the requirements, and your life as a developer will be miserable if you don't put a stop to that. Of course, it will be easier to bring this to the client (or to your manager) if you had tried to put workarounds in place, yet somehow you seem reluctant to do that either. That I don't understand at all - it's your job to find solutions to technical problems, after all.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!