Q:

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

By default Swing uses ellipses "..." to indicate that text has been truncated in JLabel and similar text based components. Is it possible to change this behavior to use a different character string, for example ">"?
Looking through the Swing code I found in SwingUtilities2 a method called clipString(...) which appears to hardcode the string to be "...". 

A:

The solution I used goes like this:
1) Register a custom LabelUI
2) The LabelUI overrides the layoutCL() method to replace the Swing ellipses with user supplied ellipses.
public class LabelUI extends MetalLabelUI {
    private static final int SWING_ELLIPSES_LEN = "...".length();
    private static final ComponentUI LABEL_UI_INSTANCE = new LabelUI();
    private String displayedText;
    public static ComponentUI createUI(JComponent c) {
        return LABEL_UI_INSTANCE;
    }
    public String getText() {
        return displayedText;
    }
    protected String layoutCL(JLabel label, FontMetrics fontMetrics, String text, Icon icon, Rectangle viewR, Rectangle iconR, Rectangle textR) {
        displayedText = super.layoutCL(label, fontMetrics, text, icon, viewR, iconR, textR);
        String truncationString = (String)label.getClientProperty("LabelUI.truncationString");
        if (truncationString != null && !displayedText.equals(text)) {
            displayedText = displayedText.subSequence(0, displayedText.length() - SWING_ELLIPSES_LEN) + truncationString;
        }
        return displayedText;
    }
}

3) Supply the new ellipses by setting a client property on the component.
JLabel component = ... ;
component.putClientProperty("LabelUI.truncationString", ">");

