18

In Java when the text of the JLabel could not be displayed due to lack of space the text is truncated and "..." is added in the end.

How can I easily find out if currently JLabel displays full text or the truncated?


EDIT:

I see that there is a way to find out the size of the text by using FontMetrics. However this solution doesn't fully answers the question. In the case the text of JLabel contains HTML decorations the metrics.stringWidth() would also calculate width of HTML tags. So it could happen that result of metrics.stringWidth() would be grater than JLabel's width but still the text would be displayed correctly.

Is there a way know what decision took the JLabel itself while displaying the text. Has it decided to truncate the text or not.

6 Answers 6

8

The ellipsis is added by the label's UI delegate, typically a subclass of BasicLabelUI, as part of it's layout and preferred size calculation. The method layoutCL() may be overridden to examine the geometry, as shown on this example.

As a practical matter, I'd ignore the elision and show the full text in a tool tip.

Sign up to request clarification or add additional context in comments.

3 Comments

show the full text in a tool tip. that is almost what I'm trying to do. If the text is truncated to show the full text in tool tip.
Thanks a lot. This is a very elegant solution.
Good call on the tooltip, I'm now using that.
7

From Oracle - Measuring Text:

// get metrics from the graphics FontMetrics metrics = graphics.getFontMetrics(font); // get the height of a line of text in this font and render context int hgt = metrics.getHeight(); // get the advance of my text in this font and render context int adv = metrics.stringWidth(text); // calculate the size of a box to hold the text with some padding. Dimension size = new Dimension(adv+2, hgt+2); 

Compare size to the size of the JLabel.getSize();

13 Comments

And what if I want to ensure that all text will be displayed by the label (no trunk)? How can I garantee that without explicitly set the size of the JLabel with setXXXSize?
Sounds impossible to me. Trivially, if you have a label that is 10 px wide and the text is the complete works of Shakespeare, there is going to be some truncating unless you change the size of the JLabel.
@Heisenburg: You calculate the size of the text as Oracle described, then you use the setPreferredSize method to set the JLabel. You'll have to figure out the appropriate padding constants in the 4th line for a JLabel.
@Kane: I asked that because it's wrong to use setXXXSize methods from the application code: stackoverflow.com/questions/7229226/… . So I was wondering what could be a solution without using those methods.
@Heisenbug: It's not wrong to set the size of elements like JLabel. It is wrong to try and override the size of JPanels. Like everything else in programming, it depends on what you're trying to accomplish with your GUI.
|
5

I suppose if the component's preferred size is greater than it's actual size, then you can expect truncation. In order for this to work, of course, the component must already be realized.

4 Comments

I'm not sure. If I set the preferred size that is grater than needed to fully display the text it could be that actual size is less than preferred but still the text is not truncated.
@jutky, It is not recommended that you explicitly set the preferred size. That's something you ought to leave to the layout manager.
So how should I bound the size of components? I taught that this is what preferred size for.
Another case where it would break: If the component's getPreferredSize() is overridden to return a smaller value in order to prevent the layout going crazy if someone sets in a longer string.
3

Check this and see the layoutCompoundLabel() method. It returns a String representing the text of the label. You can compare it to the original to determine if it will be clipped.

Jim S.

2 Comments

It looks good, but from where do I get the values for viewR, iconR, textR variables?
I passed in label.getBounds() for all three and it seemed to work correctly.
1

The usual way to do this is to use a method that calculates the expected size of the text as it will be displayed in the label. If you're using a monospaced font, this is easy:

lengthOfChar * numChars 

If you're not using a monospaced font, it's obviously much harder. I think there are some utilities around that will attempted to calculate this.

Once you have the size of the displayed string, you can compare to the length of the JLabel and see if the label is too small.

Comments

0

To get sizes of html text check this https://www.java.net/node/665691.

View view = (View) javax.swing.plaf.basic.BasicHTML.createHTMLView(label, value.toString()); int width = (int) view.getPreferredSpan(View.X_AXIS); int height = (int) view.getPreferredSpan(View.Y_AXIS); 

The only small problem is that it might have issues with non-html text. So, just use font metrics for non-html strings. The following worked perfectly for me:

if (value.toString().startsWith("<html>")) { View view = (View) javax.swing.plaf.basic.BasicHTML.createHTMLView(label, value.toString()); width = (int) view.getPreferredSpan(View.X_AXIS); } else { width = (int) label.getFontMetrics(label.getFont()).stringWidth(value.toString()); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.