How to set width of Combo or Text to match its TextLimit [message #446184] |
Fri, 19 November 2004 10:15  |
Eclipse User |
|
|
|
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 09:39   |
Eclipse User |
|
|
|
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 #446684 is a reply to message #446682] |
Tue, 30 November 2004 10:53  |
Eclipse User |
|
|
|
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.
>
|
|
|
Powered by
FUDForum. Page generated in 0.03612 seconds