|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
| Grant Gayed
Registered: July 2009
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
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.
"Steve Harper" <firstname.lastname@example.org> wrote in message
> 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
> 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?
Powered by FUDForum
. Page generated in 0.02115 seconds