开发者

How to modify the clip string ellipses used by Swing when laying out text components

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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜