>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.