I have a JLabel
that needs to display some html-formatted text. However, I want to restrict this to being 4 lines long (and if so, provide a button to see everything).
So far, I've tried setting the maximum size manually or via a layout manager. However, both of these solutions can cause part of a line to be displayed.
edit: To add a little more details, I need to force 4 lines even when respecting line wrapping correctly, resizing components, and changing font sizes. I've considered handling resize/fontsize changes by replacing the label with a new one that fits correctly.
JLabel
seems to handle incomplete tags well, so I could probably do something like a binary search on the input string finding which character would cause it to go over the 4 line limit (using FontMetric
to determine how many pixels 4 lines would be), and then replacing an existing label with the new one. The big downside to开发者_JAVA百科 this approach is that I need to run the computation every time the user resizes the panel or changes fonts (and it feels like a dirty dirty hack).
Add the JLabel to a JScrollPane as set the scrollpane with a reasonable preferred size. Scrollbars will appear a necessary.
I don't know of any absolute solution to the questions since I doubt you can define what a "line" is. One line of text may be font 12 and another 24. I don't know of any way to calculate the height of each given line.
Even if you did use a ComponentListener to handle the componentResized() event I'm not sure you can come up with a reasonable algorithm to to calculate the exact width/height of of a 4 line display.
I would try running through the String of text and removing all text after the third "\n"
String shortenText(String oldtext){
String newText = "";
for(int i=0;i<3;i++){
newText += oldtext.substring(0,oldtext.indexOf("\n"));//adds one line to String
oldtext = oldtext.substring(indexOf("\n")+1);//shorten old string to prepare for next iteration
}
return newText;
}
You may also want to try the same algorithm, except strip of <p>
and <br>
tags as well...
If you know the values of the possible tags just switch the text from "\n" to "<br>
" or any tag you need
Hey, I found a way that works. The framework I'm working with allows me to create a listener for font size changes. In this listener, I determine what the new max size of the label is (getFontMetrics(font).getHeight() * 4
) and then re-set the maximum height on the label to this and then relayout everything. This even handles the word wrap case well. I'm guessing that someone could do nasty things with silly HTML input, but this covers the 99% case pretty well.
精彩评论