how to show only text which fits the visible area in the JTextComponent
Hi,
Is there an easy way to show only the text which fits the visible part of the component in JTextComponent derived classes?
I don't need to use scrollbars and i don't like to show text which is half visible.
I mean that when the text line is half visible than i don't need to show it.
Thanks.
Slava.
[343 byte] By [
slaksa] at [2007-11-27 1:37:15]

# 1
I would do like this:
I make a JTextComponent that I want with the size I want and then a JTextComponent with the text I may want to show (with the size of the text).
If the second is "bigger" (compare PreferredSizes) than the first one, I do not show the text. Else, I put the text on the JTextCoponent I want.
I belive it might work.
hope I could help
# 2
may be i explained the problem not so correctly.
i have text component of the predefined size. and i have all kind of text - small and large. more than that it is html based text. when i make the text visible the last line visible text be showed fully, or can be clipped because it doesn't fit the component visible part
The hard way is to subclass JTextPane or JTextEditor and to control how the text is displayed using some kind of LineBreakMeasurer or TextLayout by myself... I wonder whether there is an easy way :)
# 3
Well, I still don't understand your question.
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.
# 4
For me all you need is to detect the last visible (fully visible) offset in the document and remove the rest.
Just call viewToModel() passing x=1, y=yourJTextPaneHeight.
It will return an offset (start of last visible row. Then just remove all content after the offset from your document.
Or use modelToView(offset-1). you will get a caret rectangle for the last visible row. Save y+height and add a clip to cut all content after the y+height.
regards,
Stas