tags:

views:

36

answers:

2

By default Swing uses ellipses ... to indicate that text in a text field has been truncated. Is it possible to modify this behavior so that Swing uses a different clip string? For example to use > instead of ...

SwingUtilities2.java has a method called clipString() that appears to perform this truncation and addition of "...". Unfortunately the clip string appears to be hard-coded.

A: 

SwingUtilities2 has a lot of static methods. You could (try to) copy and paste the source code of this class to a private utility class and adjust the clipString to your needs.

Or do some 'post-processing' and replace the the ellipse in the truncated result string.

For all those other cases, where clipString() is called from the Swing framework, I guess your lost.

Andreas_D
A: 

It's probably not that hard to perform the clipping yourself.

If you want to display the text in, say a JLabel, you simply get hold of the FontMetrics (which has a nice charsWidth method) and add characters until it doesn't fit, then simply backtrack as far as needed and add a '>'.

Overload suitable methods such as setSize, setFont and so on, and recompute the string that should be displayed.

aioobe