Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2005

Tip: Looking for answers? Try searching our database.

how to honour insets in paintComponent

Thread view: 
Tarlika Elisabeth Schmitz - 03 Feb 2005 15:30 GMT
Hello,

I have created a custom double-line button and overridden paintComponent().
This works fine but I've got one wee niggle: if the layout manager
squashes the button below the width returned by getPreferredSize(), my
text spills into the insets, i.e. occupies the full button width.

How do I
a) honour the insets
b) do I know where to cut the last bit of text off to replace it by an
ellipsis.

paintComponent()
{
<snip>
g.drawString(line1,offset,baseline1);
g.drawString(line2,offset,baseline2);
}

BTW, an HTML button is useless in my scenario because I have designed
the double-line text to wrap depending on the actual width (which it
does correctly).

Signature

Regards/Gruß,

Tarlika Elisabeth Schmitz

Thomas Weidenfeller - 03 Feb 2005 15:57 GMT
> How do I
> a) honour the insets
> b) do I know where to cut the last bit of text off to replace it by an
> ellipsis.

AbstractButton.setMargin()

/Thomas

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq

Tarlika Elisabeth Schmitz - 03 Feb 2005 16:51 GMT
Hallo Thomas,

>> How do I
>> a) honour the insets
>> b) do I know where to cut the last bit of text off to replace it by an
>> ellipsis.
>
> AbstractButton.setMargin()

The problem is *not* that the button has no margin; the problem is that
graphics.drawString(...) ignores it unlike the "normal" paintComponent()
method.

If the text is to big for the width, drawString utilizes the whole width
including the insets/margin. If I comment my paintComponent() out, the
the text does *not* spill into the margin. Secondly, the text is cut off
and ends in an ellipsis.

The margins are there [top=16,left=7,bottom=18,right=7]. I just couldn't
find out how the UI manages to confine a text that is too big within the
rectangle defined by the insets.

Signature

Regards/Gruß,

Tarlika

Thomas Weidenfeller - 04 Feb 2005 08:11 GMT
> 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.

/Thomas

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq

Tarlika Elisabeth Schmitz - 04 Feb 2005 20:26 GMT
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

Andrey Kuznetsov - 03 Feb 2005 16:03 GMT
> I have created a custom double-line button and overridden
> paintComponent().

I think that right way is to create double-line Border instead.

Signature

Andrey Kuznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.