Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Layout not based on content of control
Layout not based on content of control [message #498938] Thu, 19 November 2009 09:49 Go to next message
JD  is currently offline JD Friend
Messages: 3
Registered: September 2009
Junior Member
Hi there,

is there a way to make controls compute their preferred size not based on their content or make a layout ignore the control's preferred size and use the constraints provided in the layout data only?

Background:
I've got a Scrolled Form which contains a multi-line Text in a Section. The Text has a vertical scroll-bar only and the wrap bit set. The Text is inside a GridLayout and has a GridData, with SWT.FILL on the horizontal alignment and a fixed vertical size.
Entering a long text without any whitespace will cause the control to increase it's horizontal size dramatically when the View is re-layouted (happens when opening and closing sections of a the form for example).

Cheers,

Josef
Re: Layout not based on content of control [message #498939 is a reply to message #498938] Thu, 19 November 2009 09:54 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Probably using a FormLayout might help?

Tom

JD schrieb:
> Hi there,
>
> is there a way to make controls compute their preferred size not based
> on their content or make a layout ignore the control's preferred size
> and use the constraints provided in the layout data only?
>
> Background:
> I've got a Scrolled Form which contains a multi-line Text in a Section.
> The Text has a vertical scroll-bar only and the wrap bit set. The Text
> is inside a GridLayout and has a GridData, with SWT.FILL on the
> horizontal alignment and a fixed vertical size. Entering a long text
> without any whitespace will cause the control to increase it's
> horizontal size dramatically when the View is re-layouted (happens when
> opening and closing sections of a the form for example).
>
> Cheers,
>
> Josef
Re: Layout not based on content of control [message #498944 is a reply to message #498939] Thu, 19 November 2009 10:16 Go to previous messageGo to next message
JD  is currently offline JD Friend
Messages: 3
Registered: September 2009
Junior Member
Tom Schindl wrote on Thu, 19 November 2009 04:54
Probably using a FormLayout might help?

Tom



Thanks.

From a quick look into the JavaDoc, I think not, since there is nothing that would prevent the Text from growing vertically in the rest of the Form, so the Text would still be the dominant control.

On another note, I'd like to prevent exchanging the layout algorithm, since it would affect quite large portions of the code.

In the meantime I found a sort of workaround here in Example 4.
Basically you set the size hints to what they would be for the empty control.
It works in my case, but I'm, still looking for a proper way to do this, as the workaround does not work in cases, where the content is assigned before the layout data.
Re: Layout not based on content of control [message #499014 is a reply to message #498944] Thu, 19 November 2009 14:37 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
Hi,

Setting a widthHint and/or heightHint in the control's GridData is the
proper way to specify a control size that's != its preferred size. The
approach that you listed in your first message sounded right. I don't know
much about ScrolledForm and Section since they come from ui forms, maybe
they're doing something to not consider your set GridDatas? Are you able to
provide a reproducable case that shows your problem (ideal template:
http://www.eclipse.org/swt/faq.php#whatisasnippet )?

Regarding the workaround, you can determine the size of an empty Text at any
time with "Text text = new Text(parent, style); Point size =
text.computeSize(SWT.DEFAULT,SWT.DEFAULT);text.dispose();".

Grant


"JD" <duschl@creamteam.de> wrote in message
news:he35tr$l8d$1@build.eclipse.org...
> Tom Schindl wrote on Thu, 19 November 2009 04:54
> > Probably using a FormLayout might help?
> >
> > Tom
>
>
> Thanks.
>
> From a quick look into the JavaDoc, I think not, since there is nothing
that would prevent the Text from growing vertically in the rest of the Form,
so the Text would still be the dominant control.
>
> On another note, I'd like to prevent exchanging the layout algorithm,
since it would affect quite large portions of the code.
>
> In the meantime I found a sort of workaround
http://help.eclipse.org/galileo/topic/org.eclipse.platform.d oc.isv/reference/api/org/eclipse/jface/layout/GridDataFactor y.html
in Example 4.
> Basically you set the size hints to what they would be for the empty
control.
> It works in my case, but I'm, still looking for a proper way to do this,
as the workaround does not work in cases, where the content is assigned
before the layout data.
Re: Layout not based on content of control [message #499189 is a reply to message #499014] Fri, 20 November 2009 10:55 Go to previous messageGo to next message
JD  is currently offline JD Friend
Messages: 3
Registered: September 2009
Junior Member
Hi Grant,

There already is reproducing code here (well sort of):
http://www.eclipse.org/articles/Article-Forms/article.html
(Relevant section highlighted)

I think I can put together a snipped next week though, if you like.

Grant Gayed wrote on Thu, 19 November 2009 09:37
Hi,

Setting a widthHint and/or heightHint in the control's GridData is the
proper way to specify a control size that's != its preferred size. The
approach that you listed in your first message sounded right. I don't know
much about ScrolledForm and Section since they come from ui forms, maybe
they're doing something to not consider your set GridDatas? Are you able to
provide a reproducable case that shows your problem (ideal template:
http://www.eclipse.org/swt/faq.php#whatisasnippet )?

Regarding the workaround, you can determine the size of an empty Text at any
time with "Text text = new Text(parent, style); Point size =
text.computeSize(SWT.DEFAULT,SWT.DEFAULT);text.dispose();".

Grant


Re: Layout not based on content of control [message #499260 is a reply to message #499189] Fri, 20 November 2009 15:24 Go to previous message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
The snippet below is based on the one from the forms article. It updates
the Link's GridData's width hint, and therefore where it wraps, based on
size changes in its parent.

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.forms.widgets.*;

public class Main0 {
public static void main(String[] args) {
final Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(200, 200, 400, 400);
shell.setLayout(new FillLayout());
FormToolkit toolkit = new FormToolkit(shell.getDisplay());
Form form = toolkit.createForm(shell);
form.setText("Hello, Eclipse Forms");
GridLayout layout = new GridLayout();
form.getBody().setLayout(layout);
final Hyperlink link = toolkit.createHyperlink(form.getBody(), "",
SWT.WRAP);
link.setText("This is an example of a form that is much longer and will
need to wrap. " +
"This is an example of a form that is much longer and will need to
wrap.");
link.setLayoutData(new GridData());
link.getParent().addListener(SWT.Resize, new Listener() { // <----
public void handleEvent(Event event) {
Rectangle rect = link.getParent().getClientArea();
((GridData)link.getLayoutData()).widthHint = rect.width;
link.getParent().layout();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
shell.dispose();
display.dispose();
}
}

HTH,
Grant


"JD" <duschl@creamteam.de> wrote in message
news:he5sj5$sui$1@build.eclipse.org...
> Hi Grant,
>
> There already is reproducing code here (well sort of):
> http://www.eclipse.org/articles/Article-Forms/article.html
>
http://209.85.129.132/search?q=cache:cwaR0Mind7sJ:www.eclips e.org/articles/Article-Forms/article.html+inurl:http://www.e clipse.org/articles/Article-Form
s/article.html+%22Although+we+instructed+the+control+to+wrap ,+it+did+not+mat
ter+because+GridLayout+requires+that+a+control+return+its+si ze+in+isolation%
22&cd=1&hl=en&ct=clnk
>
> I think I can put together a snipped next week though, if you like.
>
> Grant Gayed wrote on Thu, 19 November 2009 09:37
> > Hi,
> >
> > Setting a widthHint and/or heightHint in the control's GridData is the
> > proper way to specify a control size that's != its preferred size. The
> > approach that you listed in your first message sounded right. I don't
know
> > much about ScrolledForm and Section since they come from ui forms, maybe
> > they're doing something to not consider your set GridDatas? Are you
able to
> > provide a reproducable case that shows your problem (ideal template:
> > http://www.eclipse.org/swt/faq.php#whatisasnippet )?
> >
> > Regarding the workaround, you can determine the size of an empty Text at
any
> > time with "Text text = new Text(parent, style); Point size =
> > text.computeSize(SWT.DEFAULT,SWT.DEFAULT);text.dispose();".
> >
> > Grant
>
>
Previous Topic:Drop Target Not Accepting Drops
Next Topic:SWT app running as collection of plugins, not JARs?
Goto Forum:
  


Current Time: Wed Apr 24 18:02:43 GMT 2024

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

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

Back to the top