@Override
    public int getWidth(final String str) {
        if (str.length() == 0) {
            return 0;
        }

        // Note: BitmapFont is now fixed to return the proper line width
        //       at least for now.  The older commented out (by someone else, not me)
        //       code below is arguably 'more accurate' if BitmapFont gets
        //       buggy again.  The issue is that the BitmapText and BitmapFont
        //       use a different algorithm for calculating size and both must
        //       be modified in sync.
        int result = (int) font.getLineWidth(str);
//        text.setText(str);
//        text.updateLogicalState(0);
//        int result = (int) text.getLineWidth();

        return result;
    }
get font width of the given string.
@param str text
@return width of the given text for the current font
jme3-niftygui/src/main/java/com/jme3/niftygui/RenderFontJme.java