Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » Question about width of SWT component
Question about width of SWT component [message #323906] Fri, 11 January 2008 16:35 Go to next message
Eclipse UserFriend
Originally posted by: ku_long.hotmail.com

Hey,

I have a text SWT component on a UI. I want the text
component can fill the horizon of the default parent
composite. Thus, I create a GridData(GridData.FILL_HORIZONTAL)
and set it to the text component. What I want is if
I resize the parent composite, I want the text component
can fill the horizon automatically. But it turned out
that when I call text.SetText(String), the width of the
text component is always automatically changed to
fit the length of the text. So, if the string is very
long, the width of the parent composite is enlarged.
How can I avoid this?

Thanks.
Re: Question about width of SWT component [message #323907 is a reply to message #323906] Fri, 11 January 2008 17:32 Go to previous messageGo to next message
Eclipse UserFriend
Raymond wrote:
> Hey,
>
> I have a text SWT component on a UI. I want the text
> component can fill the horizon of the default parent composite. Thus, I
> create a GridData(GridData.FILL_HORIZONTAL)
> and set it to the text component. What I want is if I resize the parent
> composite, I want the text component
> can fill the horizon automatically. But it turned out that when I call
> text.SetText(String), the width of the text component is always
> automatically changed to fit the length of the text. So, if the string
> is very long, the width of the parent composite is enlarged. How can I
> avoid this?
> Thanks.
>

Grid data has both an alignment property and a "grab" property:
* The alignment fields (left|top, center, right|bottom, fill) specify
where to place the control *within* the cell.
* The "grab" property specifies whether the column/row that the control
is in should grab the excess horizontal/vertical space in the composite.

So you want to use SWT.FILL for horizontal alignment and "true" for
horizontal grab property:

text.setLayoutData(new GridData(SWT.FILL, SWT.DEFAULT, true, false));

Hope this helps.

Matthew
Re: Question about width of SWT component [message #323950 is a reply to message #323907] Mon, 14 January 2008 11:38 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: eclipse-news.rizzoweb.com

Matthew Hall wrote:
> Raymond wrote:
>> Hey,
>>
>> I have a text SWT component on a UI. I want the text
>> component can fill the horizon of the default parent composite. Thus,
>> I create a GridData(GridData.FILL_HORIZONTAL)
>> and set it to the text component. What I want is if I resize the
>> parent composite, I want the text component
>> can fill the horizon automatically. But it turned out that when I call
>> text.SetText(String), the width of the text component is always
>> automatically changed to fit the length of the text. So, if the string
>> is very long, the width of the parent composite is enlarged. How can I
>> avoid this?
>> Thanks.
>>
>
> Grid data has both an alignment property and a "grab" property:
> * The alignment fields (left|top, center, right|bottom, fill) specify
> where to place the control *within* the cell.
> * The "grab" property specifies whether the column/row that the control
> is in should grab the excess horizontal/vertical space in the composite.
>
> So you want to use SWT.FILL for horizontal alignment and "true" for
> horizontal grab property:

[copying the SWT newsgroup, as that is the more appropriate forum]

GridData.FILL_HORIZONTAL is already doing exactly that.

The reason the parent control is growing is probably because it's own
layout data (and the layout of its parent) are not putting any
limitation on the size of the parent. Without a size limitation, it will
grow to accommodate the preferred size of its children, in your case a
Text widget, whose preferred size is always going to be wide enough to
display the whole text contents. So you need something to bound the size
of the Text's parent (usually by setting the layout of its parent and
its layout data appropriately).

Hope this helps,
Eric
Re: Question about width of SWT component [message #323960 is a reply to message #323950] Mon, 14 January 2008 13:41 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: ku_long.hotmail.com

Eric, I think you are right. But I don't know how to set the layout of
the parent (a composite in my case) of the text component to avoid that.
I tried lots of things but did not find anything worked.
Another Question I found [message #323961 is a reply to message #323906] Mon, 14 January 2008 14:02 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: ku_long.hotmail.com

When I tried to use ScrolledComposite as the parent of the
text wedget with the same layout and layout data, magically
the problem disappeared. When the text string is very long,
a herizonal scroll bar is shown and the width of the parent
is not enlarged to fit the string. But I got another problem,
if I move the mouse cursor to the text wedge, and press "End"
key. The cursor moved to the end of the text string which is
a expected behavior but the herizonal scroll bar did not scroll
to the end of the text. I need to manually scroll it. Do I need
to set something to make the scroll bar move automatically?

Thanks.
Re: Question about width of SWT component [message #323963 is a reply to message #323960] Mon, 14 January 2008 14:45 Go to previous messageGo to next message
Eclipse UserFriend
You have several choices, but I think you probably want to set a widthHint
in your Text's GridData().

You could then just let the scrolling happen and the user needs to drag the
text to see it:
Text text = new Text (shell, SWT.BORDER);
GridData data = new GridData (SWT.FILL, SWT.CENTER, true, false);
data.widthHint = 100;
text.setLayoutData (data);

Or you could create your Text using SWT.WRAP style:
Text text = new Text (shell, SWT.BORDER | SWT.WRAP);
GridData data = new GridData (SWT.FILL, SWT.CENTER, true, false);
data.widthHint = 100;
text.setLayoutData (data);

Or you could give your Text a scroll bar:
Text text = new Text (shell, SWT.BORDER | SWT.H_SCROLL);
GridData data = new GridData (SWT.FILL, SWT.CENTER, true, false);
data.widthHint = 100;
text.setLayoutData (data);

Hope this helps,
Carolyn

"Raymond" <ku_long@hotmail.com> wrote in message
news:f0bf9c0112d4a42ad2dad143c710199b$1@www.eclipse.org...
> Eric, I think you are right. But I don't know how to set the layout of
> the parent (a composite in my case) of the text component to avoid that.
> I tried lots of things but did not find anything worked.
>
Re: Question about width of SWT component [message #323964 is a reply to message #323950] Mon, 14 January 2008 14:47 Go to previous messageGo to next message
Eclipse UserFriend
Just a reminder to try out the LayoutExample. :)
You can then get an idea of the capabilities of GridLayout, etc.
http://www.eclipse.org/swt/examples.php

Carolyn

"Eric Rizzo" <eclipse-news@rizzoweb.com> wrote in message
news:fmg350$r9k$1@build.eclipse.org...
> Matthew Hall wrote:
>> Raymond wrote:
>>> Hey,
>>>
>>> I have a text SWT component on a UI. I want the text
>>> component can fill the horizon of the default parent composite. Thus, I
>>> create a GridData(GridData.FILL_HORIZONTAL)
>>> and set it to the text component. What I want is if I resize the parent
>>> composite, I want the text component
>>> can fill the horizon automatically. But it turned out that when I call
>>> text.SetText(String), the width of the text component is always
>>> automatically changed to fit the length of the text. So, if the string
>>> is very long, the width of the parent composite is enlarged. How can I
>>> avoid this?
>>> Thanks.
>>>
>>
>> Grid data has both an alignment property and a "grab" property:
>> * The alignment fields (left|top, center, right|bottom, fill) specify
>> where to place the control *within* the cell.
>> * The "grab" property specifies whether the column/row that the control
>> is in should grab the excess horizontal/vertical space in the composite.
>>
>> So you want to use SWT.FILL for horizontal alignment and "true" for
>> horizontal grab property:
>
> [copying the SWT newsgroup, as that is the more appropriate forum]
>
> GridData.FILL_HORIZONTAL is already doing exactly that.
>
> The reason the parent control is growing is probably because it's own
> layout data (and the layout of its parent) are not putting any limitation
> on the size of the parent. Without a size limitation, it will grow to
> accommodate the preferred size of its children, in your case a Text
> widget, whose preferred size is always going to be wide enough to display
> the whole text contents. So you need something to bound the size of the
> Text's parent (usually by setting the layout of its parent and its layout
> data appropriately).
>
> Hope this helps,
> Eric
Re: Question about width of SWT component [message #323965 is a reply to message #323964] Mon, 14 January 2008 14:49 Go to previous messageGo to next message
Eclipse UserFriend
Of course, to get the full benefit of the updated LayoutExample, you need to
be running the latest Integration build of Eclipse.
Carolyn

"Carolyn MacLeod" <Carolyn_MacLeod@ca.ibm.com> wrote in message
news:fmge9e$lhs$1@build.eclipse.org...
> Just a reminder to try out the LayoutExample. :)
> You can then get an idea of the capabilities of GridLayout, etc.
> http://www.eclipse.org/swt/examples.php
>
> Carolyn
>
> "Eric Rizzo" <eclipse-news@rizzoweb.com> wrote in message
> news:fmg350$r9k$1@build.eclipse.org...
>> Matthew Hall wrote:
>>> Raymond wrote:
>>>> Hey,
>>>>
>>>> I have a text SWT component on a UI. I want the text
>>>> component can fill the horizon of the default parent composite. Thus, I
>>>> create a GridData(GridData.FILL_HORIZONTAL)
>>>> and set it to the text component. What I want is if I resize the parent
>>>> composite, I want the text component
>>>> can fill the horizon automatically. But it turned out that when I call
>>>> text.SetText(String), the width of the text component is always
>>>> automatically changed to fit the length of the text. So, if the string
>>>> is very long, the width of the parent composite is enlarged. How can I
>>>> avoid this?
>>>> Thanks.
>>>>
>>>
>>> Grid data has both an alignment property and a "grab" property:
>>> * The alignment fields (left|top, center, right|bottom, fill) specify
>>> where to place the control *within* the cell.
>>> * The "grab" property specifies whether the column/row that the control
>>> is in should grab the excess horizontal/vertical space in the composite.
>>>
>>> So you want to use SWT.FILL for horizontal alignment and "true" for
>>> horizontal grab property:
>>
>> [copying the SWT newsgroup, as that is the more appropriate forum]
>>
>> GridData.FILL_HORIZONTAL is already doing exactly that.
>>
>> The reason the parent control is growing is probably because it's own
>> layout data (and the layout of its parent) are not putting any limitation
>> on the size of the parent. Without a size limitation, it will grow to
>> accommodate the preferred size of its children, in your case a Text
>> widget, whose preferred size is always going to be wide enough to display
>> the whole text contents. So you need something to bound the size of the
>> Text's parent (usually by setting the layout of its parent and its layout
>> data appropriately).
>>
>> Hope this helps,
>> Eric
>
>
Re: Question about width of SWT component [message #323967 is a reply to message #323963] Mon, 14 January 2008 15:07 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: ku_long.hotmail.com

Carolyn, thank you for your answer.
I think I don't want to set the data.widthHint to limit
the width of the text wedget. I want it can fill the
herizon when its parent's width changed but don't want
its width changed when its text string is too long.

I will take a look at the SWT samples.

Thanks.
Re: Question about width of SWT component [message #323968 is a reply to message #323960] Mon, 14 January 2008 15:11 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: eclipse-news.rizzoweb.com

Raymond wrote:
> Eric, I think you are right. But I don't know how to set the layout of
> the parent (a composite in my case) of the text component to avoid that.
> I tried lots of things but did not find anything worked.
>

This may help:
http://www.eclipse.org/articles/Article-Understanding-Layout s/Understanding-Layouts.htm

I find that a GUI design tool, such as WindowBuilder Pro, saves a lot of
time when trying to get detailed layout exactly right.

Hope this helps,
Eric
Re: Question about width of SWT component [message #323970 is a reply to message #323967] Mon, 14 January 2008 16:11 Go to previous messageGo to next message
Eclipse UserFriend
Try it. :)

Carolyn

"Raymond" <ku_long@hotmail.com> wrote in message
news:75895655e383d188a411e94f0d3a36ad$1@www.eclipse.org...
> Carolyn, thank you for your answer. I think I don't want to set the
> data.widthHint to limit the width of the text wedget. I want it can fill
> the herizon when its parent's width changed but don't want its width
> changed when its text string is too long.
>
> I will take a look at the SWT samples.
>
> Thanks.
>
Re: Question about width of SWT component [message #323979 is a reply to message #323967] Tue, 15 January 2008 06:56 Go to previous message
Eclipse UserFriend
Raymond,

I had the same problem. What Carolyn is suggesting, will fix it. (It's a
widthHint, not a width limit.) You also still need to set fill + grab on
the GridData object for your Text.

Rgds,
Jasper.


Raymond wrote:
> Carolyn, thank you for your answer. I think I don't want to set the
> data.widthHint to limit the width of the text wedget. I want it can fill
> the herizon when its parent's width changed but don't want its width
> changed when its text string is too long.
>
> I will take a look at the SWT samples.
>
> Thanks.
>
Previous Topic:Any example for a Form Editor to edit Objects
Next Topic:Workspace location - saving at first use
Goto Forum:
  


Current Time: Sat Jul 19 19:48:58 EDT 2025

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

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

Back to the top