SWT FillLayout [message #272268] |
Thu, 16 July 2009 05:49  |
Eclipse User |
|
|
|
Hi NG!
how can i dynamically add components to a layout (eg FillLayout)?
i was only able to find samples for "static" gui code.
it would be great if someone could point me to a sample.
thanks in advance
lumo
PS: static sample
<code>
setLayout(new FillLayout(SWT.HORIZONTAL));
{
key = new Label(this, SWT.NONE);
key.setText("description");
}
{
value = new Text(this, SWT.BORDER);
value.setText("<--data-->");
}
</code>
i was hoping for something like:
<code>
setLayout(new FillLayout(SWT.HORIZONTAL));
{
key = new Label(this, SWT.NONE);
key.setText("description");
this.getLayout().addComponent(key)
}
{
value = new Text(this, SWT.BORDER);
value.setText("<--data-->");
this.getLayout().addComponent(value)
}
</code>
so i can add components also in loops...
|
|
|
Re: SWT FillLayout [message #272277 is a reply to message #272268] |
Thu, 16 July 2009 09:59   |
Eclipse User |
|
|
|
Originally posted by: eclipse-news.rizzoweb.com
Ludwig Moser wrote:
> Hi NG!
>
> how can i dynamically add components to a layout (eg FillLayout)?
> i was only able to find samples for "static" gui code.
>
> it would be great if someone could point me to a sample.
>
> thanks in advance
> lumo
>
> PS: static sample
> <code>
> setLayout(new FillLayout(SWT.HORIZONTAL));
> {
> key = new Label(this, SWT.NONE);
> key.setText("description");
> }
> {
> value = new Text(this, SWT.BORDER);
> value.setText("<--data-->");
> }
> </code>
>
> i was hoping for something like:
> <code>
> setLayout(new FillLayout(SWT.HORIZONTAL));
> {
> key = new Label(this, SWT.NONE);
> key.setText("description");
> this.getLayout().addComponent(key)
> }
> {
> value = new Text(this, SWT.BORDER);
> value.setText("<--data-->");
> this.getLayout().addComponent(value)
> }
> </code>
> so i can add components also in loops...
You don't add components to a Layout; once a Composite has a Layout
assigned, that layout will automatically arrange all child components of
that Composite.
So for example a loop could look something like this:
setLayout(new FillLayout(SWT.HORIZONTAL));
for (int i=0; i<5 ;i++) {
new Label(this, SWT.NONE).setText("Label " + i);
new Text(this, SWT.BORDER).setText("data " + i);
}
(note that is just an example; it would not necessarily produce a
desirable visual result).
Note that if you add components to a Composite after it has been drawn
on-screen, you'll need to call Composite.layout() on it to force the
layout to recalculate and draw the new widget.
Hope this helps,
Eric
|
|
|
Re: SWT FillLayout [message #272292 is a reply to message #272277] |
Fri, 17 July 2009 01:15   |
Eclipse User |
|
|
|
thanks!
this helped a lot!
so my class looks like this now (works just fine!):
NOTE: DataControl = Composite with a label and a textfield on it
public class FormControl extends Composite {
List<KeyValue> controls = new ArrayList<KeyValue>();
/**
* Create the composite.
*
* @param parent
* @param style
*/
public FormControl(Composite parent, int style) {
super(parent, style);
}
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
public void addControl(String id, String key, String value) {
controls.add(new KeyValue(id, key, value));
}
public void generate() {
setLayout(new FillLayout(SWT.VERTICAL));
System.err.println(controls.size());
for (int i = 0; i < controls.size(); i++) {
DataControl dc = new DataControl(this, SWT.None);
dc.setSize(this.getSize().y, 21);// set size to 21 height
dc.setId(controls.get(i).id);
dc.setKey(controls.get(i).key);
dc.setValue(controls.get(i).value);
System.err.println("[" + i + "]" + controls.get(i).id + "->"
+ controls.get(i).key + "->" + controls.get(i).value);
}
}
protected class KeyValue {
public String id = "";
public String key = "";
public String value = "";
/**
* @param key
* @param value
*/
public KeyValue(String id, String key, String value) {
super();
this.id = id;
this.key = key;
this.value = value;
}
}
}
|
|
|
|
Powered by
FUDForum. Page generated in 0.08407 seconds