Set the apparent size of a JTextPane in inches?

Hi everybody,

I'm trying to make a JTextPane that can in theory look just like a printed page. I know how to make margins by adding extra panels to the sides of a BorderLayout, but the problem I'm running into (with those panels too actually) is that the setSize methods seem to only work with pixels, not inches. To be clear, I don't want the size of the JTextPane to be 6 inches wide, for example--I just want it to act as if it were 6 inches wide (think how word processors can show the width of a page and scale things accordingly). Basically what I'm trying to do is write a custom document viewer that can show and flip through pages as if they were in a book, so there would be two of these pages side by side. Any help on this would be wonderful; I have a feeling there's either a very easy way to do this or a method I've overlooked.

Thanks!

Jezzica85

[891 byte] By [jezzica85a] at [2007-11-27 8:04:33]
# 1

First of all you don't set the size of the text pane you set the preferred size. The preferred size is always specified in pixels so I guess you need to know the resolution of your monitor. You can use the getScreenResolution() method of the Toolkit class. So then I guess you just multiply this value by 6 to get the width of your text pane.

camickra at 2007-7-12 19:46:57 > top of Java-index,Desktop,Core GUI APIs...
# 2

OK, let me see if I understand. The getScreenResolution() method would return a number in pixels per inch, right? So then, that would be however many pixels in an inch, and then use that value.

Sorry if that sounds redundant or silly; I just want to make absolutely sure that I get it. Does that make a JTextPane that actually is 6 inches wide, or one that can hold what would be 6 inches of text? That second thing is what I'm after.

Thanks!

Jezzica85

jezzica85a at 2007-7-12 19:46:57 > top of Java-index,Desktop,Core GUI APIs...
# 3

> Does that make a JTextPane that actually is 6 inches wide, or one that

> can hold what would be 6 inches of text? That second thing is what

> I'm after.

it's 6 inches wide at screen.

> can hold what would be 6 inches of text?

i think you want to make it exactly same with printing.

if you use the same point size of font, i think they will look same, because font size is in point. one point = 1/72 inches. so you don't

need to worry about the resolution.

j_shadinataa at 2007-7-12 19:46:57 > top of Java-index,Desktop,Core GUI APIs...
# 4

Thanks for explaining that first part shadinata. The second part of your reply made me realize I wasn't being clear (thanks again for that). What I'd like to do is squeeze what would be 6.5 inches by 9 inches of text in whatever size JTextPane I specify. So, basically, the amount of text in the JTextPane would always be the same, no matter how big the panel was. I don't care about how it prints; this is only going to be something for the computer screen.

Of course, I could be misinterpreting your reply also.

Thanks again,

Jezzica85

jezzica85a at 2007-7-12 19:46:57 > top of Java-index,Desktop,Core GUI APIs...
# 5

> What I'd like to do is squeeze what would be 6.5 inches by 9 inches of text in whatever size JTextPane I specify

I guess I still don't really understand your question either. What does 6.5 x 9 inches of text mean?

The size of the text will depend on the font used.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.

camickra at 2007-7-12 19:46:57 > top of Java-index,Desktop,Core GUI APIs...
# 6

I haven't written this part of the program yet; I'm still working on an XML parser. I'm trying to get a general idea for how to do this, that's all, because it didn't seem like looking at the javadocs helped.

Anyway, I'll try to explain it one more time; if that doesn't work then tell me and I'll try to work through the javadocs again.

So...

When an 8.5 by 11 inch sheet of paper has 1 inch margins all around, the resulting room that's left for text is 6.5 by 9 inches. When you type text in that area, it's laid out a certain way--lines have soft returns at certain places because of word wrapping.

My application is going to take up the whole screen of a computer, and is supposed to simulate two 8.5 by 11 sheets side-by-side. So, the actual size of the JTextPanes (and therefore the font inside them) will vary based on the size of the computer screen. Each of the two JTextPanes, and the font inside them, need to be sized so the equivalent of an entire printed page shows up in each text pane. It would be similar to, in Microsoft Word, asking Print Preview to show a view of 1x2 pages.

OK, I think that about does it. Hopefully that was clearer, if not, thanks a lot for trying to help.

Jezzica85

jezzica85a at 2007-7-12 19:46:57 > top of Java-index,Desktop,Core GUI APIs...
# 7

... you can manipulate the font size relative to original size.

1. save original font size when in 6.5 inches width, ie 12 points.

2. when do scaling, ie to 13 inches width, multiply your font size to the scale factor.

the scale factor is:

scale factor =new width / original width

scale factor = 13 / 6.5 = 2

the new font size is original font * scale factor = 12 * 2 = 24.

when your scale factor is not integer (floating) you can look at this method:

public Font deriveFont(float size)

however, this scaling do both x-scaling & y-scaling. so it will do scalling regardless of your textpane height.

j_shadinataa at 2007-7-12 19:46:57 > top of Java-index,Desktop,Core GUI APIs...
# 8
Cool, thanks! I think that does it. Thanks again!Jezzica85
jezzica85a at 2007-7-12 19:46:57 > top of Java-index,Desktop,Core GUI APIs...
# 9
http://java-sl.com/Pagination_In_JEditorPane.html http://java-sl.com/Scale_In_JEditorPane.htmlI think these links will be usefull.Regards,Stas
StanislavLa at 2007-7-12 19:46:57 > top of Java-index,Desktop,Core GUI APIs...