I calculate the width of a string by using the stringWidth(\"str\") method on the FontMetrics object. This method only gives me the width from the
stringWidth(\"str\")
FontMetrics
you need a graphics object.
graphics.setFont(font);
int lenghtgraphics.getFontMetrics(graphics.getFont()).stringWidth(value);