Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » How to get labels to resize themselves in a composite?
How to get labels to resize themselves in a composite? [message #451036] Tue, 22 February 2005 11:45 Go to next message
Builder Chad is currently offline Builder ChadFriend
Messages: 5
Registered: July 2009
Junior Member
Hi,

I have a ScrolledComposite (scrollFrame) which contains a composite
(noteFrame). The noteFrame composite contains a varying amount of
labels in a vertical layout that hold large amounts of text.

The net result I am looking for is this:

,----------.
| Lable1 |^|
|--------| |
| Label2 | |
|--------| |
| Label3 | |
|--------| |
| Label4 |v|
`----------'

No matter what I try, the labels dont resize properly. They should
always fit horizontally and expand vertically to accomodate thier text
contents. The problem is that they dont and text gets lost past the
boundries of the label widgets. When more labels are added to the
composite they just get squished into the space rather than causing the
composite to grow and adjust the scroll region accordingly.

Here is some of the code which comes closest to what I want:

final ScrolledComposite noteFrame = new ScrolledComposite(leftForm,
SWT.V_SCROLL | SWT.BORDER);
noteFrame.setExpandHorizontal(true);
noteFrame.setExpandVertical(true);

final Composite c2 = new Composite(noteFrame, SWT.NONE);
noteFrame.setContent(c2);
c2.setBackground(blue);

FillLayout layout = new FillLayout(SWT.VERTICAL);
c2.setLayout(layout);
Label l1 = new Label(c2, SWT.WRAP);
l1.setText("Praesent a dui ac tortor hendrerit malesuada. Aliquam
tortor vel est euismod tincidunt. Duis risus mauris, tempus at,
scelerisque non, laoreet vel, tortor. Cras pulvinar, wisi eget
condimentum rutrum, neque nibh bibendum urna, in porta arcu purus et
sem. Etiam sit amet neque ornare purus adipiscing tincidunt. Nam
bibendum ullamcorper lorem. Fusce quis wisi rhoncus urna malesuada
venenatis. Aenean sollicitudin sapien in velit. Nulla facilisi. In eget
ante. Donec blandit urna eu orci. In porta faucibus sem. Aliquam
pharetra, ligula a auctor vulputate, arcu orci tempor dolor, ut
dignissim nisl velit vitae turpis. Integer iaculis, neque ac sodales
eleifend, sem est tempor dui, eget porttitor elit nisl eget lacus. Etiam
ultrices diam vitae massa. Class aptent taciti sociosqu ad litora
torquent per conubia nostra, per inceptos hymenaeos.");
noteFrame.setMinHeight(noteFrame.getContent().computeSize(SW T.DEFAULT,SWT.DEFAULT).y);

Button add = new Button (relatedFrame, SWT.PUSH);
add.setText("add children");
final int[] index = new int[] {0};
add.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
index[0]++;
Label lx = new Label(c2, SWT.BORDER | SWT.WRAP);
lx.setText("Label" + index[0] + ". Praesent a dui ac tortor
hendrerit malesuada. Aliquam tortor vel est euismod tincidunt. Duis
risus mauris, tempus at, scelerisque non, laoreet vel, tortor. Cras
pulvinar, wisi eget condimentum rutrum, neque nibh bibendum urna, in
porta arcu purus et sem. Etiam sit amet neque ornare purus adipiscing
tincidunt. Nam bibendum ullamcorper lorem. Fusce quis wisi rhoncus urna
malesuada venenatis. Aenean sollicitudin sapien in velit. Nulla
facilisi. In eget ante. Donec blandit urna eu orci. In porta faucibus
sem. Aliquam pharetra, ligula a auctor vulputate, arcu orci tempor
dolor, ut dignissim nisl velit vitae turpis. Integer iaculis, neque ac
sodales eleifend, sem est tempor dui, eget porttitor elit nisl eget
lacus. Etiam ultrices diam vitae massa. Class aptent taciti sociosqu ad
litora torquent per conubia nostra, per inceptos hymenaeos. ");

noteFrame.setMinHeight(noteFrame.getContent().computeSize(SW T.DEFAULT,SWT.DEFAULT).y);
c2.layout();
}
});

I have tried a number of combinations and variations including other
layouts but have not come up with the solution.

I am brand-new to SWT so any help or hints would be greatly appreciated,
thanks,

Builder Chad.
Re: How to get labels to resize themselves in a composite? [message #451037 is a reply to message #451036] Tue, 22 February 2005 12:25 Go to previous messageGo to next message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
1) Use a GridLayout on c2 (or a FormLayout) instead of a FillLayout.
2) When computing the size of c2, make sure you use the current width of the
parent client area. If you use SWT.DEFAULT, you are calculating the height
as if the text is all on one line.
3) Added a resize listener on noteFrame since the height will vary as the
width varies because the text wraps to a different extent.

public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new GridLayout(2, false));

final ScrolledComposite noteFrame = new ScrolledComposite(shell,
SWT.V_SCROLL | SWT.BORDER);
noteFrame.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
true));
noteFrame.setExpandHorizontal(true);
noteFrame.setExpandVertical(true);

final Composite c2 = new Composite(noteFrame, SWT.NONE);
noteFrame.setContent(c2);
c2.setBackground(display.getSystemColor(SWT.COLOR_BLUE));

GridLayout layout = new GridLayout();
c2.setLayout(layout);
Label l1 = new Label(c2, SWT.WRAP);
l1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
l1.setText("Praesent a dui ac tortor hendrerit malesuada. Aliquam "+
"tortor vel est euismod tincidunt. Duis risus mauris, tempus
at, "+
"scelerisque non, laoreet vel, tortor. Cras pulvinar, wisi
eget "+
"condimentum rutrum, neque nibh bibendum urna, in porta arcu
purus et "+
"sem. Etiam sit amet neque ornare purus adipiscing
tincidunt. Nam "+
"bibendum ullamcorper lorem. Fusce quis wisi rhoncus urna
malesuada "+
"venenatis. Aenean sollicitudin sapien in velit. Nulla
facilisi. In eget "+
"ante. Donec blandit urna eu orci. In porta faucibus sem.
Aliquam "+
"pharetra, ligula a auctor vulputate, arcu orci tempor
dolor, ut "+
"dignissim nisl velit vitae turpis. Integer iaculis, neque
ac sodales "+
"eleifend, sem est tempor dui, eget porttitor elit nisl eget
lacus. Etiam "+
"ultrices diam vitae massa. Class aptent taciti sociosqu ad
litora "+
"torquent per conubia nostra, per inceptos hymenaeos.");

Button add = new Button (shell, SWT.PUSH);
add.setText("add children");
final int[] index = new int[] {0};
add.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
index[0]++;
Label lx = new Label(c2, SWT.BORDER | SWT.WRAP);
lx.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
true, true));
lx.setText("Label" + index[0] + ". Praesent a dui ac
tortor "+
"hendrerit malesuada. Aliquam tortor vel est
euismod tincidunt. Duis "+
"risus mauris, tempus at, scelerisque non,
laoreet vel, tortor. Cras "+
"pulvinar, wisi eget condimentum rutrum,
neque nibh bibendum urna, in "+
"porta arcu purus et sem. Etiam sit amet
neque ornare purus adipiscing "+
"tincidunt. Nam bibendum ullamcorper lorem.
Fusce quis wisi rhoncus urna "+
"malesuada venenatis. Aenean sollicitudin
sapien in velit. Nulla "+
"facilisi. In eget ante. Donec blandit urna
eu orci. In porta faucibus "+
"sem. Aliquam pharetra, ligula a auctor
vulputate, arcu orci tempor "+
"dolor, ut dignissim nisl velit vitae
turpis. Integer iaculis, neque ac "+
"sodales eleifend, sem est tempor dui, eget
porttitor elit nisl eget "+
"lacus. Etiam ultrices diam vitae massa.
Class aptent taciti sociosqu ad "+
"litora torquent per conubia nostra, per
inceptos hymenaeos. ");

Point size =
c2.computeSize(noteFrame.getClientArea().width, SWT.DEFAULT);
noteFrame.setMinHeight(size.y);
c2.layout();
}
});

noteFrame.addListener(SWT.Resize, new Listener() {
public void handleEvent (Event e) {
Point size =
c2.computeSize(noteFrame.getClientArea().width, SWT.DEFAULT);
noteFrame.setMinHeight(size.y);
c2.layout();
}
});
shell.setSize(500, 500);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}


"Builder Chad" <chad@zulu.net> wrote in message
news:cvf5qg$g4e$1@www.eclipse.org...
>
> Hi,
>
> I have a ScrolledComposite (scrollFrame) which contains a composite
> (noteFrame). The noteFrame composite contains a varying amount of labels
> in a vertical layout that hold large amounts of text.
>
> The net result I am looking for is this:
>
> ,----------.
> | Lable1 |^|
> |--------| |
> | Label2 | |
> |--------| |
> | Label3 | |
> |--------| |
> | Label4 |v|
> `----------'
>
> No matter what I try, the labels dont resize properly. They should always
> fit horizontally and expand vertically to accomodate thier text contents.
> The problem is that they dont and text gets lost past the boundries of the
> label widgets. When more labels are added to the composite they just get
> squished into the space rather than causing the composite to grow and
> adjust the scroll region accordingly.
>
> Here is some of the code which comes closest to what I want:
>
> final ScrolledComposite noteFrame = new ScrolledComposite(leftForm,
> SWT.V_SCROLL | SWT.BORDER);
> noteFrame.setExpandHorizontal(true);
> noteFrame.setExpandVertical(true);
>
> final Composite c2 = new Composite(noteFrame, SWT.NONE);
> noteFrame.setContent(c2);
> c2.setBackground(blue);
>
> FillLayout layout = new FillLayout(SWT.VERTICAL);
> c2.setLayout(layout);
> Label l1 = new Label(c2, SWT.WRAP);
> l1.setText("Praesent a dui ac tortor hendrerit malesuada. Aliquam tortor
> vel est euismod tincidunt. Duis risus mauris, tempus at, scelerisque non,
> laoreet vel, tortor. Cras pulvinar, wisi eget condimentum rutrum, neque
> nibh bibendum urna, in porta arcu purus et sem. Etiam sit amet neque
> ornare purus adipiscing tincidunt. Nam bibendum ullamcorper lorem. Fusce
> quis wisi rhoncus urna malesuada venenatis. Aenean sollicitudin sapien in
> velit. Nulla facilisi. In eget ante. Donec blandit urna eu orci. In porta
> faucibus sem. Aliquam pharetra, ligula a auctor vulputate, arcu orci
> tempor dolor, ut dignissim nisl velit vitae turpis. Integer iaculis, neque
> ac sodales eleifend, sem est tempor dui, eget porttitor elit nisl eget
> lacus. Etiam ultrices diam vitae massa. Class aptent taciti sociosqu ad
> litora torquent per conubia nostra, per inceptos hymenaeos.");
> noteFrame.setMinHeight(noteFrame.getContent().computeSize(SW T.DEFAULT,SWT.DEFAULT).y);
> Button add = new Button (relatedFrame, SWT.PUSH);
> add.setText("add children");
> final int[] index = new int[] {0};
> add.addListener(SWT.Selection, new Listener() {
> public void handleEvent(Event e) {
> index[0]++;
> Label lx = new Label(c2, SWT.BORDER | SWT.WRAP);
> lx.setText("Label" + index[0] + ". Praesent a dui ac tortor hendrerit
> malesuada. Aliquam tortor vel est euismod tincidunt. Duis risus mauris,
> tempus at, scelerisque non, laoreet vel, tortor. Cras pulvinar, wisi eget
> condimentum rutrum, neque nibh bibendum urna, in porta arcu purus et sem.
> Etiam sit amet neque ornare purus adipiscing tincidunt. Nam bibendum
> ullamcorper lorem. Fusce quis wisi rhoncus urna malesuada venenatis.
> Aenean sollicitudin sapien in velit. Nulla facilisi. In eget ante. Donec
> blandit urna eu orci. In porta faucibus sem. Aliquam pharetra, ligula a
> auctor vulputate, arcu orci tempor dolor, ut dignissim nisl velit vitae
> turpis. Integer iaculis, neque ac sodales eleifend, sem est tempor dui,
> eget porttitor elit nisl eget lacus. Etiam ultrices diam vitae massa.
> Class aptent taciti sociosqu ad litora torquent per conubia nostra, per
> inceptos hymenaeos. ");
>
> noteFrame.setMinHeight(noteFrame.getContent().computeSize(SW T.DEFAULT,SWT.DEFAULT).y);
> c2.layout();
> }
> });
> I have tried a number of combinations and variations including other
> layouts but have not come up with the solution.
>
> I am brand-new to SWT so any help or hints would be greatly appreciated,
> thanks,
>
> Builder Chad.
Re: How to get labels to resize themselves in a composite? [message #451039 is a reply to message #451037] Tue, 22 February 2005 13:16 Go to previous messageGo to next message
Builder Chad is currently offline Builder ChadFriend
Messages: 5
Registered: July 2009
Junior Member
Thanks Veronika,

I will definately play with the gridlayout. I managed to get your code
snippet running with some difficulty as I had some trouble with the
GridData stuff. For one I couldn't find SWT.FILL no matter what I
imported but I came across GridData.FILL so I hope that is the same
thing. Also I couldnt use the GridData constructor as you wrote it even
though according to the manual what you wrote is 100% correct and I am
using the latest SWT library. In fact no matter what I put as
parameters, it complained that that constructor was undefined. I ended
up typing:

GridData gd = new GridData();
gd.grabExcessHorizontalSpace = true;
gd.grabExcessVerticalSpace = true;
gd.horizontalAlignment = GridData.FILL;
gd.verticalAlignment = GridData.FILL;
noteFrame.setLayoutData(gd);

I think I may have a faulty setup with SWT.

I will mess around with this more as it seems to be getting closer to
what I need for the horizontal wrapping. However, new labels still get
squashed into the client area, each taking up equal amounts of space
rather than expand vertically which is what I need. So text disappears
off the bottom of each label rather than the label growing in size to
display all the text. The scroll bar only appears if I resize the
window to really small.

Thanks for your help as this has given me a lot more to experiment with.
If you have any other help or comments I would gladly appreciate them.

Builder Chad.


Veronika Irvine wrote:
> 1) Use a GridLayout on c2 (or a FormLayout) instead of a FillLayout.
> 2) When computing the size of c2, make sure you use the current width of the
> parent client area. If you use SWT.DEFAULT, you are calculating the height
> as if the text is all on one line.
> 3) Added a resize listener on noteFrame since the height will vary as the
> width varies because the text wraps to a different extent.
>
> public static void main (String [] args) {
> Display display = new Display ();
> Shell shell = new Shell (display);
> shell.setLayout(new GridLayout(2, false));
>
> final ScrolledComposite noteFrame = new ScrolledComposite(shell,
> SWT.V_SCROLL | SWT.BORDER);
> noteFrame.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
> true));
> noteFrame.setExpandHorizontal(true);
> noteFrame.setExpandVertical(true);
>
> final Composite c2 = new Composite(noteFrame, SWT.NONE);
> noteFrame.setContent(c2);
> c2.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
>
> GridLayout layout = new GridLayout();
> c2.setLayout(layout);
> Label l1 = new Label(c2, SWT.WRAP);
> l1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
> l1.setText("Praesent a dui ac tortor hendrerit malesuada. Aliquam "+
> "tortor vel est euismod tincidunt. Duis risus mauris, tempus
> at, "+
> "scelerisque non, laoreet vel, tortor. Cras pulvinar, wisi
> eget "+
> "condimentum rutrum, neque nibh bibendum urna, in porta arcu
> purus et "+
> "sem. Etiam sit amet neque ornare purus adipiscing
> tincidunt. Nam "+
> "bibendum ullamcorper lorem. Fusce quis wisi rhoncus urna
> malesuada "+
> "venenatis. Aenean sollicitudin sapien in velit. Nulla
> facilisi. In eget "+
> "ante. Donec blandit urna eu orci. In porta faucibus sem.
> Aliquam "+
> "pharetra, ligula a auctor vulputate, arcu orci tempor
> dolor, ut "+
> "dignissim nisl velit vitae turpis. Integer iaculis, neque
> ac sodales "+
> "eleifend, sem est tempor dui, eget porttitor elit nisl eget
> lacus. Etiam "+
> "ultrices diam vitae massa. Class aptent taciti sociosqu ad
> litora "+
> "torquent per conubia nostra, per inceptos hymenaeos.");
>
> Button add = new Button (shell, SWT.PUSH);
> add.setText("add children");
> final int[] index = new int[] {0};
> add.addListener(SWT.Selection, new Listener() {
> public void handleEvent(Event e) {
> index[0]++;
> Label lx = new Label(c2, SWT.BORDER | SWT.WRAP);
> lx.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
> true, true));
> lx.setText("Label" + index[0] + ". Praesent a dui ac
> tortor "+
> "hendrerit malesuada. Aliquam tortor vel est
> euismod tincidunt. Duis "+
> "risus mauris, tempus at, scelerisque non,
> laoreet vel, tortor. Cras "+
> "pulvinar, wisi eget condimentum rutrum,
> neque nibh bibendum urna, in "+
> "porta arcu purus et sem. Etiam sit amet
> neque ornare purus adipiscing "+
> "tincidunt. Nam bibendum ullamcorper lorem.
> Fusce quis wisi rhoncus urna "+
> "malesuada venenatis. Aenean sollicitudin
> sapien in velit. Nulla "+
> "facilisi. In eget ante. Donec blandit urna
> eu orci. In porta faucibus "+
> "sem. Aliquam pharetra, ligula a auctor
> vulputate, arcu orci tempor "+
> "dolor, ut dignissim nisl velit vitae
> turpis. Integer iaculis, neque ac "+
> "sodales eleifend, sem est tempor dui, eget
> porttitor elit nisl eget "+
> "lacus. Etiam ultrices diam vitae massa.
> Class aptent taciti sociosqu ad "+
> "litora torquent per conubia nostra, per
> inceptos hymenaeos. ");
>
> Point size =
> c2.computeSize(noteFrame.getClientArea().width, SWT.DEFAULT);
> noteFrame.setMinHeight(size.y);
> c2.layout();
> }
> });
>
> noteFrame.addListener(SWT.Resize, new Listener() {
> public void handleEvent (Event e) {
> Point size =
> c2.computeSize(noteFrame.getClientArea().width, SWT.DEFAULT);
> noteFrame.setMinHeight(size.y);
> c2.layout();
> }
> });
> shell.setSize(500, 500);
> shell.open ();
> while (!shell.isDisposed ()) {
> if (!display.readAndDispatch ()) display.sleep ();
> }
> display.dispose ();
> }
>
>
> "Builder Chad" <chad@zulu.net> wrote in message
> news:cvf5qg$g4e$1@www.eclipse.org...
>
>>Hi,
>>
>>I have a ScrolledComposite (scrollFrame) which contains a composite
>>(noteFrame). The noteFrame composite contains a varying amount of labels
>>in a vertical layout that hold large amounts of text.
>>
>>The net result I am looking for is this:
>>
>>,----------.
>>| Lable1 |^|
>>|--------| |
>>| Label2 | |
>>|--------| |
>>| Label3 | |
>>|--------| |
>>| Label4 |v|
>>`----------'
>>
>>No matter what I try, the labels dont resize properly. They should always
>>fit horizontally and expand vertically to accomodate thier text contents.
>>The problem is that they dont and text gets lost past the boundries of the
>>label widgets. When more labels are added to the composite they just get
>>squished into the space rather than causing the composite to grow and
>>adjust the scroll region accordingly.
>>
>>Here is some of the code which comes closest to what I want:
>>
>>final ScrolledComposite noteFrame = new ScrolledComposite(leftForm,
>>SWT.V_SCROLL | SWT.BORDER);
>>noteFrame.setExpandHorizontal(true);
>>noteFrame.setExpandVertical(true);
>>
>>final Composite c2 = new Composite(noteFrame, SWT.NONE);
>>noteFrame.setContent(c2);
>>c2.setBackground(blue);
>>
>>FillLayout layout = new FillLayout(SWT.VERTICAL);
>>c2.setLayout(layout);
>>Label l1 = new Label(c2, SWT.WRAP);
>>l1.setText("Praesent a dui ac tortor hendrerit malesuada. Aliquam tortor
>>vel est euismod tincidunt. Duis risus mauris, tempus at, scelerisque non,
>>laoreet vel, tortor. Cras pulvinar, wisi eget condimentum rutrum, neque
>>nibh bibendum urna, in porta arcu purus et sem. Etiam sit amet neque
>>ornare purus adipiscing tincidunt. Nam bibendum ullamcorper lorem. Fusce
>>quis wisi rhoncus urna malesuada venenatis. Aenean sollicitudin sapien in
>>velit. Nulla facilisi. In eget ante. Donec blandit urna eu orci. In porta
>>faucibus sem. Aliquam pharetra, ligula a auctor vulputate, arcu orci
>>tempor dolor, ut dignissim nisl velit vitae turpis. Integer iaculis, neque
>>ac sodales eleifend, sem est tempor dui, eget porttitor elit nisl eget
>>lacus. Etiam ultrices diam vitae massa. Class aptent taciti sociosqu ad
>>litora torquent per conubia nostra, per inceptos hymenaeos.");
>> noteFrame.setMinHeight(noteFrame.getContent().computeSize(SW T.DEFAULT,SWT.DEFAULT).y);
>>Button add = new Button (relatedFrame, SWT.PUSH);
>>add.setText("add children");
>>final int[] index = new int[] {0};
>>add.addListener(SWT.Selection, new Listener() {
>> public void handleEvent(Event e) {
>>index[0]++;
>>Label lx = new Label(c2, SWT.BORDER | SWT.WRAP);
>> lx.setText("Label" + index[0] + ". Praesent a dui ac tortor hendrerit
>>malesuada. Aliquam tortor vel est euismod tincidunt. Duis risus mauris,
>>tempus at, scelerisque non, laoreet vel, tortor. Cras pulvinar, wisi eget
>>condimentum rutrum, neque nibh bibendum urna, in porta arcu purus et sem.
>>Etiam sit amet neque ornare purus adipiscing tincidunt. Nam bibendum
>>ullamcorper lorem. Fusce quis wisi rhoncus urna malesuada venenatis.
>>Aenean sollicitudin sapien in velit. Nulla facilisi. In eget ante. Donec
>>blandit urna eu orci. In porta faucibus sem. Aliquam pharetra, ligula a
>>auctor vulputate, arcu orci tempor dolor, ut dignissim nisl velit vitae
>>turpis. Integer iaculis, neque ac sodales eleifend, sem est tempor dui,
>>eget porttitor elit nisl eget lacus. Etiam ultrices diam vitae massa.
>>Class aptent taciti sociosqu ad litora torquent per conubia nostra, per
>>inceptos hymenaeos. ");
>>
>> noteFrame.setMinHeight(noteFrame.getContent().computeSize(SW T.DEFAULT,SWT.DEFAULT).y);
>>c2.layout();
>> }
>> });
>> I have tried a number of combinations and variations including other
>>layouts but have not come up with the solution.
>>
>>I am brand-new to SWT so any help or hints would be greatly appreciated,
>>thanks,
>>
>>Builder Chad.
>
>
>
Re: How to get labels to resize themselves in a composite? [message #451040 is a reply to message #451039] Tue, 22 February 2005 13:32 Go to previous messageGo to next message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
Sounds like you are using an old version of SWT. GridLayout was rewritten
in 3.1 (M4) to support wrapping. If you have an older version of SWT you
will need to set a widthHint on the GridData in order to get wrapping to
work.

"Builder Chad" <chad@zulu.net> wrote in message
news:cvfb4p$cci$1@www.eclipse.org...
> Thanks Veronika,
>
> I will definately play with the gridlayout. I managed to get your code
> snippet running with some difficulty as I had some trouble with the
> GridData stuff. For one I couldn't find SWT.FILL no matter what I
> imported but I came across GridData.FILL so I hope that is the same thing.
> Also I couldnt use the GridData constructor as you wrote it even though
> according to the manual what you wrote is 100% correct and I am using the
> latest SWT library. In fact no matter what I put as parameters, it
> complained that that constructor was undefined. I ended up typing:
>
> GridData gd = new GridData();
> gd.grabExcessHorizontalSpace = true;
> gd.grabExcessVerticalSpace = true;
> gd.horizontalAlignment = GridData.FILL;
> gd.verticalAlignment = GridData.FILL;
> noteFrame.setLayoutData(gd);
>
> I think I may have a faulty setup with SWT.
>
> I will mess around with this more as it seems to be getting closer to what
> I need for the horizontal wrapping. However, new labels still get
> squashed into the client area, each taking up equal amounts of space
> rather than expand vertically which is what I need. So text disappears
> off the bottom of each label rather than the label growing in size to
> display all the text. The scroll bar only appears if I resize the window
> to really small.
>
> Thanks for your help as this has given me a lot more to experiment with.
> If you have any other help or comments I would gladly appreciate them.
>
> Builder Chad.
>
>
> Veronika Irvine wrote:
>> 1) Use a GridLayout on c2 (or a FormLayout) instead of a FillLayout.
>> 2) When computing the size of c2, make sure you use the current width of
>> the parent client area. If you use SWT.DEFAULT, you are calculating the
>> height as if the text is all on one line.
>> 3) Added a resize listener on noteFrame since the height will vary as the
>> width varies because the text wraps to a different extent.
>>
>> public static void main (String [] args) {
>> Display display = new Display ();
>> Shell shell = new Shell (display);
>> shell.setLayout(new GridLayout(2, false));
>>
>> final ScrolledComposite noteFrame = new ScrolledComposite(shell,
>> SWT.V_SCROLL | SWT.BORDER);
>> noteFrame.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
>> true));
>> noteFrame.setExpandHorizontal(true);
>> noteFrame.setExpandVertical(true);
>>
>> final Composite c2 = new Composite(noteFrame, SWT.NONE);
>> noteFrame.setContent(c2);
>> c2.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
>>
>> GridLayout layout = new GridLayout();
>> c2.setLayout(layout);
>> Label l1 = new Label(c2, SWT.WRAP);
>> l1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
>> l1.setText("Praesent a dui ac tortor hendrerit malesuada. Aliquam
>> "+
>> "tortor vel est euismod tincidunt. Duis risus mauris,
>> tempus at, "+
>> "scelerisque non, laoreet vel, tortor. Cras pulvinar,
>> wisi eget "+
>> "condimentum rutrum, neque nibh bibendum urna, in porta
>> arcu purus et "+
>> "sem. Etiam sit amet neque ornare purus adipiscing
>> tincidunt. Nam "+
>> "bibendum ullamcorper lorem. Fusce quis wisi rhoncus urna
>> malesuada "+
>> "venenatis. Aenean sollicitudin sapien in velit. Nulla
>> facilisi. In eget "+
>> "ante. Donec blandit urna eu orci. In porta faucibus sem.
>> Aliquam "+
>> "pharetra, ligula a auctor vulputate, arcu orci tempor
>> dolor, ut "+
>> "dignissim nisl velit vitae turpis. Integer iaculis,
>> neque ac sodales "+
>> "eleifend, sem est tempor dui, eget porttitor elit nisl
>> eget lacus. Etiam "+
>> "ultrices diam vitae massa. Class aptent taciti sociosqu
>> ad litora "+
>> "torquent per conubia nostra, per inceptos hymenaeos.");
>>
>> Button add = new Button (shell, SWT.PUSH);
>> add.setText("add children");
>> final int[] index = new int[] {0};
>> add.addListener(SWT.Selection, new Listener() {
>> public void handleEvent(Event e) {
>> index[0]++;
>> Label lx = new Label(c2, SWT.BORDER | SWT.WRAP);
>> lx.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
>> true, true));
>> lx.setText("Label" + index[0] + ". Praesent a dui
>> ac tortor "+
>> "hendrerit malesuada. Aliquam tortor vel
>> est euismod tincidunt. Duis "+
>> "risus mauris, tempus at, scelerisque
>> non, laoreet vel, tortor. Cras "+
>> "pulvinar, wisi eget condimentum rutrum,
>> neque nibh bibendum urna, in "+
>> "porta arcu purus et sem. Etiam sit amet
>> neque ornare purus adipiscing "+
>> "tincidunt. Nam bibendum ullamcorper
>> lorem. Fusce quis wisi rhoncus urna "+
>> "malesuada venenatis. Aenean sollicitudin
>> sapien in velit. Nulla "+
>> "facilisi. In eget ante. Donec blandit
>> urna eu orci. In porta faucibus "+
>> "sem. Aliquam pharetra, ligula a auctor
>> vulputate, arcu orci tempor "+
>> "dolor, ut dignissim nisl velit vitae
>> turpis. Integer iaculis, neque ac "+
>> "sodales eleifend, sem est tempor dui,
>> eget porttitor elit nisl eget "+
>> "lacus. Etiam ultrices diam vitae massa.
>> Class aptent taciti sociosqu ad "+
>> "litora torquent per conubia nostra, per
>> inceptos hymenaeos. ");
>>
>> Point size =
>> c2.computeSize(noteFrame.getClientArea().width, SWT.DEFAULT);
>> noteFrame.setMinHeight(size.y);
>> c2.layout();
>> }
>> });
>>
>> noteFrame.addListener(SWT.Resize, new Listener() {
>> public void handleEvent (Event e) {
>> Point size =
>> c2.computeSize(noteFrame.getClientArea().width, SWT.DEFAULT);
>> noteFrame.setMinHeight(size.y);
>> c2.layout();
>> }
>> });
>> shell.setSize(500, 500);
>> shell.open ();
>> while (!shell.isDisposed ()) {
>> if (!display.readAndDispatch ()) display.sleep ();
>> }
>> display.dispose ();
>> }
>>
>>
>> "Builder Chad" <chad@zulu.net> wrote in message
>> news:cvf5qg$g4e$1@www.eclipse.org...
>>
>>>Hi,
>>>
>>>I have a ScrolledComposite (scrollFrame) which contains a composite
>>>(noteFrame). The noteFrame composite contains a varying amount of labels
>>>in a vertical layout that hold large amounts of text.
>>>
>>>The net result I am looking for is this:
>>>
>>>,----------.
>>>| Lable1 |^|
>>>|--------| |
>>>| Label2 | |
>>>|--------| |
>>>| Label3 | |
>>>|--------| |
>>>| Label4 |v|
>>>`----------'
>>>
>>>No matter what I try, the labels dont resize properly. They should
>>>always fit horizontally and expand vertically to accomodate thier text
>>>contents. The problem is that they dont and text gets lost past the
>>>boundries of the label widgets. When more labels are added to the
>>>composite they just get squished into the space rather than causing the
>>>composite to grow and adjust the scroll region accordingly.
>>>
>>>Here is some of the code which comes closest to what I want:
>>>
>>>final ScrolledComposite noteFrame = new ScrolledComposite(leftForm,
>>>SWT.V_SCROLL | SWT.BORDER);
>>>noteFrame.setExpandHorizontal(true);
>>>noteFrame.setExpandVertical(true);
>>>
>>>final Composite c2 = new Composite(noteFrame, SWT.NONE);
>>>noteFrame.setContent(c2);
>>>c2.setBackground(blue);
>>>
>>>FillLayout layout = new FillLayout(SWT.VERTICAL);
>>>c2.setLayout(layout);
>>>Label l1 = new Label(c2, SWT.WRAP);
>>>l1.setText("Praesent a dui ac tortor hendrerit malesuada. Aliquam tortor
>>>vel est euismod tincidunt. Duis risus mauris, tempus at, scelerisque non,
>>>laoreet vel, tortor. Cras pulvinar, wisi eget condimentum rutrum, neque
>>>nibh bibendum urna, in porta arcu purus et sem. Etiam sit amet neque
>>>ornare purus adipiscing tincidunt. Nam bibendum ullamcorper lorem. Fusce
>>>quis wisi rhoncus urna malesuada venenatis. Aenean sollicitudin sapien in
>>>velit. Nulla facilisi. In eget ante. Donec blandit urna eu orci. In porta
>>>faucibus sem. Aliquam pharetra, ligula a auctor vulputate, arcu orci
>>>tempor dolor, ut dignissim nisl velit vitae turpis. Integer iaculis,
>>>neque ac sodales eleifend, sem est tempor dui, eget porttitor elit nisl
>>>eget lacus. Etiam ultrices diam vitae massa. Class aptent taciti sociosqu
>>>ad litora torquent per conubia nostra, per inceptos hymenaeos.");
>>> noteFrame.setMinHeight(noteFrame.getContent().computeSize(SW T.DEFAULT,SWT.DEFAULT).y);
>>>Button add = new Button (relatedFrame, SWT.PUSH);
>>>add.setText("add children");
>>>final int[] index = new int[] {0};
>>>add.addListener(SWT.Selection, new Listener() {
>>> public void handleEvent(Event e) {
>>>index[0]++;
>>>Label lx = new Label(c2, SWT.BORDER | SWT.WRAP);
>>> lx.setText("Label" + index[0] + ". Praesent a dui ac tortor
>>> hendrerit malesuada. Aliquam tortor vel est euismod tincidunt. Duis
>>> risus mauris, tempus at, scelerisque non, laoreet vel, tortor. Cras
>>> pulvinar, wisi eget condimentum rutrum, neque nibh bibendum urna, in
>>> porta arcu purus et sem. Etiam sit amet neque ornare purus adipiscing
>>> tincidunt. Nam bibendum ullamcorper lorem. Fusce quis wisi rhoncus urna
>>> malesuada venenatis. Aenean sollicitudin sapien in velit. Nulla
>>> facilisi. In eget ante. Donec blandit urna eu orci. In porta faucibus
>>> sem. Aliquam pharetra, ligula a auctor vulputate, arcu orci tempor
>>> dolor, ut dignissim nisl velit vitae turpis. Integer iaculis, neque ac
>>> sodales eleifend, sem est tempor dui, eget porttitor elit nisl eget
>>> lacus. Etiam ultrices diam vitae massa. Class aptent taciti sociosqu ad
>>> litora torquent per conubia nostra, per inceptos hymenaeos. ");
>>>
>>> noteFrame.setMinHeight(noteFrame.getContent().computeSize(SW T.DEFAULT,SWT.DEFAULT).y);
>>>c2.layout();
>>> }
>>> });
>>> I have tried a number of combinations and variations including other
>>> layouts but have not come up with the solution.
>>>
>>>I am brand-new to SWT so any help or hints would be greatly appreciated,
>>>thanks,
>>>
>>>Builder Chad.
>>
>>
Re: How to get labels to resize themselves in a composite? [message #451063 is a reply to message #451040] Tue, 22 February 2005 14:03 Go to previous message
Builder Chad is currently offline Builder ChadFriend
Messages: 5
Registered: July 2009
Junior Member
Thanks!! I downloaded the 'latest' version this morning thinking it was
a version thing but I got the latest stable 3.0.1 version. I went back
and indeed found version 3.1M5a and this is working exactly as I want it
to now.

Thanks again Veronika :)

Chad.

Veronika Irvine wrote:
> Sounds like you are using an old version of SWT. GridLayout was rewritten
> in 3.1 (M4) to support wrapping. If you have an older version of SWT you
> will need to set a widthHint on the GridData in order to get wrapping to
> work.
>
> "Builder Chad" <chad@zulu.net> wrote in message
> news:cvfb4p$cci$1@www.eclipse.org...
>
>>Thanks Veronika,
>>
>>I will definately play with the gridlayout. I managed to get your code
>>snippet running with some difficulty as I had some trouble with the
>>GridData stuff. For one I couldn't find SWT.FILL no matter what I
>>imported but I came across GridData.FILL so I hope that is the same thing.
>>Also I couldnt use the GridData constructor as you wrote it even though
>>according to the manual what you wrote is 100% correct and I am using the
>>latest SWT library. In fact no matter what I put as parameters, it
>>complained that that constructor was undefined. I ended up typing:
>>
>>GridData gd = new GridData();
>>gd.grabExcessHorizontalSpace = true;
>>gd.grabExcessVerticalSpace = true;
>>gd.horizontalAlignment = GridData.FILL;
>>gd.verticalAlignment = GridData.FILL;
>>noteFrame.setLayoutData(gd);
>>
>>I think I may have a faulty setup with SWT.
>>
>>I will mess around with this more as it seems to be getting closer to what
>>I need for the horizontal wrapping. However, new labels still get
>>squashed into the client area, each taking up equal amounts of space
>>rather than expand vertically which is what I need. So text disappears
>>off the bottom of each label rather than the label growing in size to
>>display all the text. The scroll bar only appears if I resize the window
>>to really small.
>>
>>Thanks for your help as this has given me a lot more to experiment with.
>>If you have any other help or comments I would gladly appreciate them.
>>
>>Builder Chad.
>>
>>
>>Veronika Irvine wrote:
>>
>>>1) Use a GridLayout on c2 (or a FormLayout) instead of a FillLayout.
>>>2) When computing the size of c2, make sure you use the current width of
>>>the parent client area. If you use SWT.DEFAULT, you are calculating the
>>>height as if the text is all on one line.
>>>3) Added a resize listener on noteFrame since the height will vary as the
>>>width varies because the text wraps to a different extent.
>>>
>>>public static void main (String [] args) {
>>> Display display = new Display ();
>>> Shell shell = new Shell (display);
>>> shell.setLayout(new GridLayout(2, false));
>>>
>>> final ScrolledComposite noteFrame = new ScrolledComposite(shell,
>>>SWT.V_SCROLL | SWT.BORDER);
>>> noteFrame.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
>>>true));
>>> noteFrame.setExpandHorizontal(true);
>>> noteFrame.setExpandVertical(true);
>>>
>>> final Composite c2 = new Composite(noteFrame, SWT.NONE);
>>> noteFrame.setContent(c2);
>>> c2.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
>>>
>>> GridLayout layout = new GridLayout();
>>> c2.setLayout(layout);
>>> Label l1 = new Label(c2, SWT.WRAP);
>>> l1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
>>> l1.setText("Praesent a dui ac tortor hendrerit malesuada. Aliquam
>>>"+
>>> "tortor vel est euismod tincidunt. Duis risus mauris,
>>>tempus at, "+
>>> "scelerisque non, laoreet vel, tortor. Cras pulvinar,
>>>wisi eget "+
>>> "condimentum rutrum, neque nibh bibendum urna, in porta
>>>arcu purus et "+
>>> "sem. Etiam sit amet neque ornare purus adipiscing
>>>tincidunt. Nam "+
>>> "bibendum ullamcorper lorem. Fusce quis wisi rhoncus urna
>>>malesuada "+
>>> "venenatis. Aenean sollicitudin sapien in velit. Nulla
>>>facilisi. In eget "+
>>> "ante. Donec blandit urna eu orci. In porta faucibus sem.
>>>Aliquam "+
>>> "pharetra, ligula a auctor vulputate, arcu orci tempor
>>>dolor, ut "+
>>> "dignissim nisl velit vitae turpis. Integer iaculis,
>>>neque ac sodales "+
>>> "eleifend, sem est tempor dui, eget porttitor elit nisl
>>>eget lacus. Etiam "+
>>> "ultrices diam vitae massa. Class aptent taciti sociosqu
>>>ad litora "+
>>> "torquent per conubia nostra, per inceptos hymenaeos.");
>>>
>>> Button add = new Button (shell, SWT.PUSH);
>>> add.setText("add children");
>>> final int[] index = new int[] {0};
>>> add.addListener(SWT.Selection, new Listener() {
>>> public void handleEvent(Event e) {
>>> index[0]++;
>>> Label lx = new Label(c2, SWT.BORDER | SWT.WRAP);
>>> lx.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
>>>true, true));
>>> lx.setText("Label" + index[0] + ". Praesent a dui
>>>ac tortor "+
>>> "hendrerit malesuada. Aliquam tortor vel
>>>est euismod tincidunt. Duis "+
>>> "risus mauris, tempus at, scelerisque
>>>non, laoreet vel, tortor. Cras "+
>>> "pulvinar, wisi eget condimentum rutrum,
>>>neque nibh bibendum urna, in "+
>>> "porta arcu purus et sem. Etiam sit amet
>>>neque ornare purus adipiscing "+
>>> "tincidunt. Nam bibendum ullamcorper
>>>lorem. Fusce quis wisi rhoncus urna "+
>>> "malesuada venenatis. Aenean sollicitudin
>>>sapien in velit. Nulla "+
>>> "facilisi. In eget ante. Donec blandit
>>>urna eu orci. In porta faucibus "+
>>> "sem. Aliquam pharetra, ligula a auctor
>>>vulputate, arcu orci tempor "+
>>> "dolor, ut dignissim nisl velit vitae
>>>turpis. Integer iaculis, neque ac "+
>>> "sodales eleifend, sem est tempor dui,
>>>eget porttitor elit nisl eget "+
>>> "lacus. Etiam ultrices diam vitae massa.
>>>Class aptent taciti sociosqu ad "+
>>> "litora torquent per conubia nostra, per
>>>inceptos hymenaeos. ");
>>>
>>> Point size =
>>>c2.computeSize(noteFrame.getClientArea().width, SWT.DEFAULT);
>>> noteFrame.setMinHeight(size.y);
>>> c2.layout();
>>> }
>>> });
>>>
>>> noteFrame.addListener(SWT.Resize, new Listener() {
>>> public void handleEvent (Event e) {
>>> Point size =
>>>c2.computeSize(noteFrame.getClientArea().width, SWT.DEFAULT);
>>> noteFrame.setMinHeight(size.y);
>>> c2.layout();
>>> }
>>> });
>>> shell.setSize(500, 500);
>>> shell.open ();
>>> while (!shell.isDisposed ()) {
>>> if (!display.readAndDispatch ()) display.sleep ();
>>> }
>>> display.dispose ();
>>>}
>>>
>>>
>>>"Builder Chad" <chad@zulu.net> wrote in message
>>>news:cvf5qg$g4e$1@www.eclipse.org...
>>>
>>>
>>>>Hi,
>>>>
>>>>I have a ScrolledComposite (scrollFrame) which contains a composite
>>>>(noteFrame). The noteFrame composite contains a varying amount of labels
>>>>in a vertical layout that hold large amounts of text.
>>>>
>>>>The net result I am looking for is this:
>>>>
>>>>,----------.
>>>>| Lable1 |^|
>>>>|--------| |
>>>>| Label2 | |
>>>>|--------| |
>>>>| Label3 | |
>>>>|--------| |
>>>>| Label4 |v|
>>>>`----------'
>>>>
>>>>No matter what I try, the labels dont resize properly. They should
>>>>always fit horizontally and expand vertically to accomodate thier text
>>>>contents. The problem is that they dont and text gets lost past the
>>>>boundries of the label widgets. When more labels are added to the
>>>>composite they just get squished into the space rather than causing the
>>>>composite to grow and adjust the scroll region accordingly.
>>>>
>>>>Here is some of the code which comes closest to what I want:
>>>>
>>>>final ScrolledComposite noteFrame = new ScrolledComposite(leftForm,
>>>>SWT.V_SCROLL | SWT.BORDER);
>>>>noteFrame.setExpandHorizontal(true);
>>>>noteFrame.setExpandVertical(true);
>>>>
>>>>final Composite c2 = new Composite(noteFrame, SWT.NONE);
>>>>noteFrame.setContent(c2);
>>>>c2.setBackground(blue);
>>>>
>>>>FillLayout layout = new FillLayout(SWT.VERTICAL);
>>>>c2.setLayout(layout);
>>>>Label l1 = new Label(c2, SWT.WRAP);
>>>>l1.setText("Praesent a dui ac tortor hendrerit malesuada. Aliquam tortor
>>>>vel est euismod tincidunt. Duis risus mauris, tempus at, scelerisque non,
>>>>laoreet vel, tortor. Cras pulvinar, wisi eget condimentum rutrum, neque
>>>>nibh bibendum urna, in porta arcu purus et sem. Etiam sit amet neque
>>>>ornare purus adipiscing tincidunt. Nam bibendum ullamcorper lorem. Fusce
>>>>quis wisi rhoncus urna malesuada venenatis. Aenean sollicitudin sapien in
>>>>velit. Nulla facilisi. In eget ante. Donec blandit urna eu orci. In porta
>>>>faucibus sem. Aliquam pharetra, ligula a auctor vulputate, arcu orci
>>>>tempor dolor, ut dignissim nisl velit vitae turpis. Integer iaculis,
>>>>neque ac sodales eleifend, sem est tempor dui, eget porttitor elit nisl
>>>>eget lacus. Etiam ultrices diam vitae massa. Class aptent taciti sociosqu
>>>>ad litora torquent per conubia nostra, per inceptos hymenaeos.");
>>>> noteFrame.setMinHeight(noteFrame.getContent().computeSize(SW T.DEFAULT,SWT.DEFAULT).y);
>>>>Button add = new Button (relatedFrame, SWT.PUSH);
>>>>add.setText("add children");
>>>>final int[] index = new int[] {0};
>>>>add.addListener(SWT.Selection, new Listener() {
>>>> public void handleEvent(Event e) {
>>>>index[0]++;
>>>>Label lx = new Label(c2, SWT.BORDER | SWT.WRAP);
>>>> lx.setText("Label" + index[0] + ". Praesent a dui ac tortor
>>>>hendrerit malesuada. Aliquam tortor vel est euismod tincidunt. Duis
>>>>risus mauris, tempus at, scelerisque non, laoreet vel, tortor. Cras
>>>>pulvinar, wisi eget condimentum rutrum, neque nibh bibendum urna, in
>>>>porta arcu purus et sem. Etiam sit amet neque ornare purus adipiscing
>>>>tincidunt. Nam bibendum ullamcorper lorem. Fusce quis wisi rhoncus urna
>>>>malesuada venenatis. Aenean sollicitudin sapien in velit. Nulla
>>>>facilisi. In eget ante. Donec blandit urna eu orci. In porta faucibus
>>>>sem. Aliquam pharetra, ligula a auctor vulputate, arcu orci tempor
>>>>dolor, ut dignissim nisl velit vitae turpis. Integer iaculis, neque ac
>>>>sodales eleifend, sem est tempor dui, eget porttitor elit nisl eget
>>>>lacus. Etiam ultrices diam vitae massa. Class aptent taciti sociosqu ad
>>>>litora torquent per conubia nostra, per inceptos hymenaeos. ");
>>>>
>>>> noteFrame.setMinHeight(noteFrame.getContent().computeSize(SW T.DEFAULT,SWT.DEFAULT).y);
>>>>c2.layout();
>>>> }
>>>> });
>>>>I have tried a number of combinations and variations including other
>>>>layouts but have not come up with the solution.
>>>>
>>>>I am brand-new to SWT so any help or hints would be greatly appreciated,
>>>>thanks,
>>>>
>>>>Builder Chad.
>>>
>>>
>
Previous Topic:Strange problem with SWT tables ...
Next Topic:FileDialog as Save As dialog
Goto Forum:
  


Current Time: Mon Sep 28 07:12:22 GMT 2020

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

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

Back to the top