Question

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.

Était-ce utile?

La solution

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.

Autres conseils

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();

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.

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.

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.

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());
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top