0

I have a problem with my Jbuttons.

In my application, you can easily change the ui language and you can easily override the default translations for my buttons.

In this case, it is very unclear how long the text can be in a button, but my buttons have a fixed size (because of graphical harmony and so on).

Now my problem is, that I haven't found a solution to wrap a text with a inner button margin.

Example:

BUTTON 1: "Hello" -> Hello is short enough to be printed without newline.

BUTTON 2: "Hello guys" -> Due to the html tags, Hello guys will be automatically wrapped in two lines.

BUTTON 3: "Hello g." -> Hello g. fills exactly the width of the button. No word wrap by HTML!.

Now, the button 3 itself looks very crappy and overloaded.

Therefore I need a solution to automatically wrap a text which is wider or equal -4px than the button.

Additionally a text which doesn't contain a whitespace, should be wrapped either if it is too long.

JavaDM
  • 853
  • 1
  • 5
  • 28
  • *"In this case, it is very unclear how long the text can be in a button, but my buttons have a fixed size (because of graphical harmony and so on)."* Use a `GridLayout` for that section of a GUI. It is designed to calculate the space needed to display the largest, then it enforces that size on all the other components. More generally, see [Is there a “word wrap” property for JLabel?](http://stackoverflow.com/questions/7861724/is-there-a-word-wrap-property-for-jlabel/7861833#7861833) (the same applies to any HTML aware `JComponent`). – Andrew Thompson Sep 16 '15 at 08:53

1 Answers1

0

A very easy solution for this problem is a utility method, I wrote.

Just call it in your *ButtonUI #paint method before you call super.paint(c,g);

e.g.:

if (c instanceof AbstractButton) {
    String txt = button.getText();
    button.setText(getWrappedText(g, button, txt));
}

Here is my formatter for free use (and for optimization, too ;) )

private static final String STR_NEWLINE = "<br />";
private FontRenderContext fontRenderContext = new FontRenderContext(new AffineTransform(), true, true);

private String getWrappedText(Graphics graphics, AbstractButton button, String str) {
    if( str != null ) {
        String text = str.replaceAll("<html><center>", "").replaceAll("</center></html>", "");
        int width = button.getWidth();
        Rectangle2D stringBounds = button.getFont().getStringBounds(text, fontRenderContext);
        if ( !str.contains(STR_NEWLINE) && (width-5) < ((Double)stringBounds.getWidth()).intValue()) {
            String newStr;
            if( str.contains(" ") ) {
                int lastIndex = str.lastIndexOf(" ");
                newStr = str.substring(0, lastIndex)+STR_NEWLINE+str.substring(lastIndex);
            } else {
                int strLength = ((str.length()/3)*2);
                newStr = str.substring(0, strLength)+STR_NEWLINE+str.substring(strLength);
            }
            return newStr;
        }
    }
    return str;
}
JavaDM
  • 853
  • 1
  • 5
  • 28