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.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
jutky
  • 3,895
  • 6
  • 31
  • 45

6 Answers6

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
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();

Aleksandr Kravets
  • 5,750
  • 7
  • 53
  • 72
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • 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? – Heisenbug Nov 01 '11 at 18:44
  • 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. – Kane Nov 01 '11 at 18:46
  • 1
    @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. – Gilbert Le Blanc Nov 01 '11 at 18:47
  • @Kane: I asked that because it's wrong to use setXXXSize methods from the application code: http://stackoverflow.com/questions/7229226/avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swing . So I was wondering what could be a solution without using those methods. – Heisenbug Nov 01 '11 at 18:48
  • @Gilbert Le Blanc: have a look at the thread I linked in my previous comment. – Heisenbug Nov 01 '11 at 18:49
  • @Heisenbug: For consistency, you can find the size of all your label text and set all of your JLabel's to the maximum text size. – Gilbert Le Blanc Nov 01 '11 at 18:49
  • @Heisenbug Well, change the font size if it's too big and test the length of the text to the size of the JLabel again until you hit a predetermined stopping size or it fits. – Brandon Buck Nov 01 '11 at 18:51
  • 1
    @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. – Gilbert Le Blanc Nov 01 '11 at 18:52
  • @Heisenbug: Your linked thread answers your question. The real question you should be asking is why your layout manager is shrinking that component so much. – Kane Nov 01 '11 at 18:54
  • @Kane: yes you are right. Sorry but I'm still a bit confused about that :) . Anyway that's true, it's the LayoutManager that should eventually resolve that case. – Heisenbug Nov 01 '11 at 19:00
  • Thanks for answers. Your answer is good, but not for all cases. If the text of the JLabel is " boo foo " - then it could be that the text would be displayed correctly, but `metrics.stringWidth()` would return a value that is grater than JLabel's width. – jutky Nov 01 '11 at 19:32
  • I want to know if there is a flag of the JLabel itself that mirrors if the text was truncated or not. Thanks. – jutky Nov 01 '11 at 19:35
  • Also, metrics.getStringBounds() should be used to deal with anti-aliasing. – Nathan Apr 27 '13 at 01:08
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.

mre
  • 43,520
  • 33
  • 120
  • 170
  • 2
    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 Nov 01 '11 at 20:01
  • @jutky, It is not recommended that you explicitly set the preferred size. That's something you ought to leave to the layout manager. – mre Nov 01 '11 at 20:06
  • 1
    So how should I bound the size of components? I taught that this is what preferred size for. – jutky Nov 01 '11 at 20:13
  • 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. – Hakanai Mar 14 '14 at 03:25
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.

Awais Qarni
  • 17,492
  • 24
  • 75
  • 137
Jim S.
  • 231
  • 1
  • 6
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.

Kane
  • 4,047
  • 2
  • 24
  • 33
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());
}
Serge
  • 11,616
  • 3
  • 18
  • 28