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 / August 2004

Tip: Looking for answers? Try searching our database.

How to determine number of visible lines

Thread view: 
stevewi - 26 Aug 2004 22:53 GMT
How do you find out the number of lines that are visible in
JEditorPane?

I'm displaying nothing but text and using a single font in the
display. I'm using a JScrollPane to handle the scrolling.

I've tried getting the viewport.getVisibleRect() and dividing it by
the font height, but it doesn't match the actual number of lines being
displayed.

Thanks!
Cid - 27 Aug 2004 07:56 GMT
>How do you find out the number of lines that are visible in
>JEditorPane?
[quoted text clipped - 5 lines]
>the font height, but it doesn't match the actual number of lines being
>displayed.

Please elaborate on "doesn't match". I don't see any other useful
methods for this so I'd approach it the same way you did. So it
probably depends on how you're counting lines.

You want to count the lines of content in the display area or the
number of possible lines that could be displayed? Counting a blank
line as a line of text? Etc. Explain more about what you have and want
to get.
stevewi - 27 Aug 2004 14:33 GMT
> >How do you find out the number of lines that are visible in
> >JEditorPane?
[quoted text clipped - 14 lines]
> line as a line of text? Etc. Explain more about what you have and want
> to get.

When I setText() to be a string that is larger then the viewing area
so that the entire view is filled and I count the number of lines
displayed, it does not match the number that is calculated by
viewport.getVisibleRect() / fontmetrics.getHeight(). It's 2/3 lines
less then what is displayed. Example, displaying 50 lines, calculation
comes up with 47. The text I'm displaying has no blank lines, in fact,
my text has line numbers to make it easy to see how many lines are
displayed.
mail@guido-zijlstra.net - 27 Aug 2004 18:13 GMT
Did you account for the 1 or 2 empty pixel-lines between the text-lines? (As
you can see, the characters don't touch eachother top or bottom so there is
a space of 1 or 2 pixels between them. So you need to divide the number of
pixel-lines through the fontheight + 1 or somethin like that).
Cid - 27 Aug 2004 19:41 GMT
>Did you account for the 1 or 2 empty pixel-lines between the text-lines? (As
>you can see, the characters don't touch eachother top or bottom so there is
>a space of 1 or 2 pixels between them. So you need to divide the number of
>pixel-lines through the fontheight + 1 or somethin like that).

Presumably this isn't the problem since his calculated line count is
already smaller than the actual. So increasing the line height in the
calculation would only reduce the calculated line count further.

OP, I assume from your examples (calc 47, actual 50) that you meant
calculation is generally 2 to 3 lines short not 2/3?

Just for kicks, you're sure you've got the right font and you got the
fontmetrics from the text area's Graphics?

As a fallback, I had the same issue with windows text controls. Best
solution there seemed to be determining which lines were visible at
the top and bottom of the control's display area (see
JTextComponent.viewToModel) and just getting the difference. That
assumes that you have discrete non-wrapping lines which sounds like it
may be true if you're inserting line numbers in the display. (?)
stevewi - 31 Aug 2004 16:39 GMT
> >Did you account for the 1 or 2 empty pixel-lines between the text-lines? (As
> >you can see, the characters don't touch eachother top or bottom so there is
[quoted text clipped - 17 lines]
> assumes that you have discrete non-wrapping lines which sounds like it
> may be true if you're inserting line numbers in the display. (?)

I wasn't getting the correct fontmetrics. I jacked up the font size
and the fontmetrics stayed the same. I was doing a
editorPane.getFont(). I had to change to...

      DefaultStyledDocument doc =
(DefaultStyledDocument)editorPane.getDocument();

       Style defaultStyle = doc.getStyle("default");
       Font f = new Font(StyleConstants.getFontFamily(defaultStyle),
Font.PLAIN,StyleConstants.getFontSize(defaultStyle) );
       FontMetrics fm = editorPane.getFontMetrics(f);

I also had to take into account the height of the horizontal scrollbar
and subtract it from
scrollPane.getViewport().getViewRect().getHeigth();

Let me know if anyone see's a problem with this.

Thanks for the help!
Cid - 31 Aug 2004 22:00 GMT
>I wasn't getting the correct fontmetrics. I jacked up the font size
>and the fontmetrics stayed the same. I was doing a
[quoted text clipped - 13 lines]
>
>Let me know if anyone see's a problem with this.

I forgot it was an editorPane instead of a text area so that alarm
didn't go off sooner. Good catch.

Your plan looks good to me - except you should be able to ignore the
horizontal scrollbar. getViewRect on the viewport should give you the
interior of the scrollpane (the scrollbars aren't part of the viewport
area). As long as you're getting the right font metrics from the
editor pane's document you should be set. This assumes that the
document lines all use the same font size of course.


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.