Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » How to set width of Combo or Text to match its TextLimit
How to set width of Combo or Text to match its TextLimit [message #446184] Fri, 19 November 2004 15:15 Go to next message
Steve Harper is currently offline Steve HarperFriend
Messages: 29
Registered: July 2009
Junior Member
In adding Combo and Text controls to a Composite with a GridLayout, wanted
each control's width to be able to accomodate up to it's TextLimit number
of characters.

Found that calling setSize() on the control immediately after it was
constructed seemed to have no effect.
What did work was calling setText() and passing a String that contained
TextLimit number of a character that is of average size width (I used
N's).
Got away with this, since the fields are immediately filled with values
from a model, so users never sees the "NNNNNNN"s.

Still I wonder if this is the only way to do this in SWT??

Also I was setting arbitrary widths when calling setSize(), since I could
find no way via the Font class to get the size of a String. If setSize()
had worked planned to call Control.getFont() and then call something like
awt's Font.getStringBounds() for the Font the Control was using. Is there
a similar SWT api?
Re: How to set width of Combo or Text to match its TextLimit [message #446193 is a reply to message #446184] Fri, 19 November 2004 20:39 Go to previous messageGo to next message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet55.java?rev=HEAD&a mp;content-type=text/vnd.viewcvs-markup

"Steve Harper" <steve_harper@adp.com> wrote in message
news:cnl2mq$slt$1@www.eclipse.org...
> In adding Combo and Text controls to a Composite with a GridLayout, wanted
> each control's width to be able to accomodate up to it's TextLimit number
> of characters.
>
> Found that calling setSize() on the control immediately after it was
> constructed seemed to have no effect.
> What did work was calling setText() and passing a String that contained
> TextLimit number of a character that is of average size width (I used
> N's).
> Got away with this, since the fields are immediately filled with values
> from a model, so users never sees the "NNNNNNN"s.
>
> Still I wonder if this is the only way to do this in SWT??
>
> Also I was setting arbitrary widths when calling setSize(), since I could
> find no way via the Font class to get the size of a String. If setSize()
> had worked planned to call Control.getFont() and then call something like
> awt's Font.getStringBounds() for the Font the Control was using. Is there
> a similar SWT api?
>
Re: How to set width of Combo or Text to match its TextLimit [message #446558 is a reply to message #446184] Fri, 26 November 2004 14:39 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
Hi Steve, sorry for the late response,

If your Composite has a GridLayout then you can specify a width hint with
Combo/Text.setLayoutData(GridData), and set the GridData's widthHint field
appropriately. Setting the "NNNNNNN"'s works for you because no hint is
currently being set, so the Combo/Text is being asked by the GridLayout for
its ideal size given its content; there's no a problem with this, it's just
a bit ugly programatically.

To determine a good width hint value, start by measuring the string as shown
in
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet93.java?rev=HEAD&a mp;content-type=text/vnd.viewcvs-markup
(create the GC on the Combo/Text so that its font will be used, and remember
to dispose the GC afterwards). You can then take this result and give it to
Combo/Text.computeTrim(...) to determine the total size that should be
allocated for the widget while considering its margins, borders, etc.

HTH,
Grant

"Steve Harper" <steve_harper@adp.com> wrote in message
news:cnl2mq$slt$1@www.eclipse.org...
> In adding Combo and Text controls to a Composite with a GridLayout, wanted
> each control's width to be able to accomodate up to it's TextLimit number
> of characters.
>
> Found that calling setSize() on the control immediately after it was
> constructed seemed to have no effect.
> What did work was calling setText() and passing a String that contained
> TextLimit number of a character that is of average size width (I used
> N's).
> Got away with this, since the fields are immediately filled with values
> from a model, so users never sees the "NNNNNNN"s.
>
> Still I wonder if this is the only way to do this in SWT??
>
> Also I was setting arbitrary widths when calling setSize(), since I could
> find no way via the Font class to get the size of a String. If setSize()
> had worked planned to call Control.getFont() and then call something like
> awt's Font.getStringBounds() for the Font the Control was using. Is there
> a similar SWT api?
>
Re: How to set width of Combo or Text to match its TextLimit [message #446682 is a reply to message #446558] Tue, 30 November 2004 15:19 Go to previous messageGo to next message
Steve Harper is currently offline Steve HarperFriend
Messages: 29
Registered: July 2009
Junior Member
Sizing using GC.textExtent() works nicely, and it's good to know about the
FontMetrics class also.
Thanks for the good advice.

Still seems strange to have to set the GridData.widthHint, would think
that the GridLayout would honor the size set into the Control.
Re: How to set width of Combo or Text to match its TextLimit [message #446684 is a reply to message #446682] Tue, 30 November 2004 15:53 Go to previous message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
It can't. The job of a layout is to position and size controls. If you
think about it, un order to do this right, it needs to disregard the size
that is already in the control because it can't tell the difference between
a size that you set and a size that it calculated.

"Steve Harper" <steve_harper@adp.com> wrote in message
news:coi31o$m97$1@www.eclipse.org...
> Sizing using GC.textExtent() works nicely, and it's good to know about the
> FontMetrics class also.
> Thanks for the good advice.
>
> Still seems strange to have to set the GridData.widthHint, would think
> that the GridLayout would honor the size set into the Control.
>
Previous Topic:gcj: installation problem, cannot exec `cc1'
Next Topic:TooltipText in List widget SWT/GTK
Goto Forum:
  


Current Time: Wed Oct 28 18:26:34 GMT 2020

Powered by FUDForum. Page generated in 0.02229 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top