
Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
Hello Thomas,
>> The problem is *not* that the button has no margin; the problem is
>> that graphics.drawString(...) ignores it unlike the "normal"
>> paintComponent() method.
>
> Set a clipping region.
Thank you for nudging me in the right direction.
Here's the long answer:
Create a Graphis context, which *only* covers the paintable area, i.e.
excludes the button's insets:
Graphics textArea =
graphics.create(INSETS.left,INSETS.top,areaWidth,areaHeight);
If the button has been squashed, the text will now be cut off before the
right margin. In this case, you want to end in an ellipsis:
So, rather than drawing the line, calculate the potentially clipped line:
int offset = (areaWidth - lineWidth) / 2;
if (offset < 0) /* line too long, shorten it */
{
offset = 0;
Rectangle paintArea = new Rectangle(areaWidth, areaHeight);
printLine =
SwingUtilities.layoutCompoundLabel(this,textArea.getFontMetrics(),
line,null,getVerticalAlignment(),getHorizontalAlignment(),
getVerticalTextPosition(),getHorizontalTextPosition(),paintArea,
new Rectangle(),new Rectangle(),0);
}
textArea.drawString(printLine,offset,baseline);
(The above code is for a text button without icon.)

Signature
Regards/Gruß,
Tarlika