Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Tip for Correct implementation to anchor a widget in the parent composite rectangle.
Tip for Correct implementation to anchor a widget in the parent composite rectangle. [message #506847] Mon, 11 January 2010 01:01 Go to next message
Eclipse UserFriend
Originally posted by: jccanova.gmail.com

Hello,

Im starting with a SWT app that have a view with a Tree (or text). This
Composite has a GridLayout with 1 column.
By default the control (three or text the 2 that im using now) is shown as
small *default size (thats ok)..

I noticed that i can apply a GridData in the Widget (since the parent has a
GridLayout) that instruct the layout manager (GridLayout) how the widget
must the displayed.
But reading the help and SWT code appear some doubts....

1 - Where is the help or tutorials that are telling the the LayoutManager
cascades through the children..? Like if the composite has a Layout
(GridLayout) i configure the children using a GridData? :) Quite similar
with CSS. But default the IPageLayout is a FillLayout ... ?

2 - Who is the receiver that is mentioned in the SWT source code ? the
Display , the Widget , what ?

3 - Why the name of the field in GridLayout is like widthHint (that if the
developer put bigger than parent client area will not obey the margins so is
not a "hint") and in FillLayout if im not wrong is width.. ?

4 - Which is the best approach to anchor controls ?

5 - What does it means the true / false in the layout methods ? like layout
(true), layout (false) .... since i dont know what is the receiver ....
:)...

Regards.

//Snippet for layout inhiterance.

package br.com.octech.jnewsreader.views;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.part.ViewPart;

public class JNewsServerView extends ViewPart {

public static final String ID = "JNewsServerView.1";
private Composite serverPanelGroup;
private Tree serverTree;

public JNewsServerView() {
}

@Override
public void createPartControl(Composite parent) {
createServerPanelGroup(parent);
}

private void createServerPanelGroup(Composite parent) {
serverPanelGroup = new Composite(parent, SWT.NONE | SWT.BORDER);
Color bgColor = new Color (serverPanelGroup.getDisplay(), new RGB(255 , 0
, 0));
serverPanelGroup.setBackground(bgColor);
GridLayout panelLayout = new GridLayout();
panelLayout.makeColumnsEqualWidth = false;
serverPanelGroup.setLayout(panelLayout);
//Here with the group if seems that grabexcess space works fine.
GridData panelGridData = new GridData(GridData.FILL_BOTH);
//Is this necessary ? or not ? in which cases i use the grabExcess....
fields?
panelGridData.grabExcessHorizontalSpace = true;
panelGridData.grabExcessVerticalSpace = true;
serverPanelGroup.setLayoutData(panelGridData);
serverPanelGroup.addListener(SWT.Paint, new GroupResizeListener());
createControls(serverPanelGroup);
}

private void createControls(Composite parent) {
createServerTree(parent);
}

private void createServerTree(Composite parent) {
serverTree = new Tree(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
serverTree.setHeaderVisible(true);
TreeItem topItem = new TreeItem(serverTree, SWT.NONE);
topItem.setText("THIS IS THE TOP ITEM");
serverTree.setTopItem(topItem);
//Since the parent is a gridlayout you can use a gridData... otherwise you
will receice a ClassCastException.
GridData treeData = new GridData(GridData.FILL_BOTH);
serverTree.setLayoutData(treeData);
//Here with the group if seems that grabexcess space works fine.
treeData.grabExcessHorizontalSpace = true;
treeData.grabExcessVerticalSpace = true;
}


/**
* @deprecated
*/
private void calculateServerTreeSize() {
Composite parent = serverTree.getParent();
GridData treeGridData = null;
if (serverTree.getLayoutData() == null) {
serverTree.setLayoutData(new GridData());
}
treeGridData = (GridData) serverTree.getLayoutData();
treeGridData.widthHint = parent.getClientArea().width -30;
treeGridData.heightHint = parent.getClientArea().height -30;
}

@Override
public void setFocus() {
serverTree.setFocus();
}

/**
* @deprecated
* @author Administrator
*
*/
class GroupResizeListener implements Listener
{

public GroupResizeListener()
{}

@Override
public void handleEvent(Event event) {
switch (event.type) {
case SWT.Paint:
Object eventSource = event.widget;
Composite composite = (Composite)eventSource;
// calculateServerTreeSize();
composite.layout(true , false);
break;
}
}
}
}
Re: Tip for Correct implementation to anchor a widget in the parent composite rectangle. [message #507034 is a reply to message #506847] Mon, 11 January 2010 17:52 Go to previous messageGo to next message
Carolyn MacLeod is currently offline Carolyn MacLeodFriend
Messages: 149
Registered: July 2009
Senior Member
Hi, Jose.

1.
http://www.eclipse.org/articles/article.php?file=Article-Und erstanding-Layouts/index.html
2. The "receiver" is always the containing class. i.e. "this".
3. Width/height are hints because the layout may not be able to honour a
requested size due to other layout constraints.
4. Try using FormLayout. Alternatively, you can just position your controls
on paint, and recalculate positions on resize (i.e. don't use a layout).
5. The Layout may or may not have a cache. If it does, then layout(true)
will clear the cache before doing the layout.

You might also want to try downloading the SWT examples and playing with the
LayoutExample: http://www.eclipse.org/swt/examples.php

Hope this helps,
Carolyn

"Jose Carlos Canova" <jccanova@gmail.com> wrote in message
news:hidtag$l2e$1@build.eclipse.org...
> Hello,
>
> Im starting with a SWT app that have a view with a Tree (or text). This
> Composite has a GridLayout with 1 column.
> By default the control (three or text the 2 that im using now) is shown as
> small *default size (thats ok)..
>
> I noticed that i can apply a GridData in the Widget (since the parent has
> a GridLayout) that instruct the layout manager (GridLayout) how the widget
> must the displayed.
> But reading the help and SWT code appear some doubts....
>
> 1 - Where is the help or tutorials that are telling the the LayoutManager
> cascades through the children..? Like if the composite has a Layout
> (GridLayout) i configure the children using a GridData? :) Quite similar
> with CSS. But default the IPageLayout is a FillLayout ... ?
>
> 2 - Who is the receiver that is mentioned in the SWT source code ? the
> Display , the Widget , what ?
>
> 3 - Why the name of the field in GridLayout is like widthHint (that if the
> developer put bigger than parent client area will not obey the margins so
> is not a "hint") and in FillLayout if im not wrong is width.. ?
>
> 4 - Which is the best approach to anchor controls ?
>
> 5 - What does it means the true / false in the layout methods ? like
> layout (true), layout (false) .... since i dont know what is the receiver
> .... :)...
>
> Regards.
>
> //Snippet for layout inhiterance.
>
> package br.com.octech.jnewsreader.views;
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.graphics.Color;
> import org.eclipse.swt.graphics.RGB;
> import org.eclipse.swt.layout.GridData;
> import org.eclipse.swt.layout.GridLayout;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Event;
> import org.eclipse.swt.widgets.Listener;
> import org.eclipse.swt.widgets.Tree;
> import org.eclipse.swt.widgets.TreeItem;
> import org.eclipse.ui.part.ViewPart;
>
> public class JNewsServerView extends ViewPart {
>
> public static final String ID = "JNewsServerView.1";
> private Composite serverPanelGroup;
> private Tree serverTree;
>
> public JNewsServerView() {
> }
>
> @Override
> public void createPartControl(Composite parent) {
> createServerPanelGroup(parent);
> }
>
> private void createServerPanelGroup(Composite parent) {
> serverPanelGroup = new Composite(parent, SWT.NONE | SWT.BORDER);
> Color bgColor = new Color (serverPanelGroup.getDisplay(), new RGB(255 , 0
> , 0));
> serverPanelGroup.setBackground(bgColor);
> GridLayout panelLayout = new GridLayout();
> panelLayout.makeColumnsEqualWidth = false;
> serverPanelGroup.setLayout(panelLayout);
> //Here with the group if seems that grabexcess space works fine.
> GridData panelGridData = new GridData(GridData.FILL_BOTH);
> //Is this necessary ? or not ? in which cases i use the grabExcess....
> fields?
> panelGridData.grabExcessHorizontalSpace = true;
> panelGridData.grabExcessVerticalSpace = true;
> serverPanelGroup.setLayoutData(panelGridData);
> serverPanelGroup.addListener(SWT.Paint, new GroupResizeListener());
> createControls(serverPanelGroup);
> }
>
> private void createControls(Composite parent) {
> createServerTree(parent);
> }
>
> private void createServerTree(Composite parent) {
> serverTree = new Tree(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
> serverTree.setHeaderVisible(true);
> TreeItem topItem = new TreeItem(serverTree, SWT.NONE);
> topItem.setText("THIS IS THE TOP ITEM");
> serverTree.setTopItem(topItem);
> //Since the parent is a gridlayout you can use a gridData... otherwise you
> will receice a ClassCastException.
> GridData treeData = new GridData(GridData.FILL_BOTH);
> serverTree.setLayoutData(treeData);
> //Here with the group if seems that grabexcess space works fine.
> treeData.grabExcessHorizontalSpace = true;
> treeData.grabExcessVerticalSpace = true;
> }
>
>
> /**
> * @deprecated
> */
> private void calculateServerTreeSize() {
> Composite parent = serverTree.getParent();
> GridData treeGridData = null;
> if (serverTree.getLayoutData() == null) {
> serverTree.setLayoutData(new GridData());
> }
> treeGridData = (GridData) serverTree.getLayoutData();
> treeGridData.widthHint = parent.getClientArea().width -30;
> treeGridData.heightHint = parent.getClientArea().height -30;
> }
>
> @Override
> public void setFocus() {
> serverTree.setFocus();
> }
>
> /**
> * @deprecated
> * @author Administrator
> *
> */
> class GroupResizeListener implements Listener
> {
>
> public GroupResizeListener()
> {}
>
> @Override
> public void handleEvent(Event event) {
> switch (event.type) {
> case SWT.Paint:
> Object eventSource = event.widget;
> Composite composite = (Composite)eventSource;
> // calculateServerTreeSize();
> composite.layout(true , false);
> break;
> }
> }
> }
> }
>
>
Re: Tip for Correct implementation to anchor a widget in the parent composite rectangle. [message #507294 is a reply to message #507034] Tue, 12 January 2010 19:13 Go to previous message
Eclipse UserFriend
Originally posted by: jccanova.gmail.com

Tks Carolyn,

The article is just great!

your explanation about the my basic questions are what i want to know to
finish my basic understanding on layouts.

Tks lot.

Jose Carlos Canova.




"Carolyn MacLeod" <Carolyn_MacLeod@ca.ibm.com> wrote in message
news:hifoi1$4vt$1@build.eclipse.org...
> Hi, Jose.
>
> 1.
> http://www.eclipse.org/articles/article.php?file=Article-Und erstanding-Layouts/index.html
> 2. The "receiver" is always the containing class. i.e. "this".
> 3. Width/height are hints because the layout may not be able to honour a
> requested size due to other layout constraints.
> 4. Try using FormLayout. Alternatively, you can just position your
> controls on paint, and recalculate positions on resize (i.e. don't use a
> layout).
> 5. The Layout may or may not have a cache. If it does, then layout(true)
> will clear the cache before doing the layout.
>
> You might also want to try downloading the SWT examples and playing with
> the LayoutExample: http://www.eclipse.org/swt/examples.php
>
> Hope this helps,
> Carolyn
>
> "Jose Carlos Canova" <jccanova@gmail.com> wrote in message
> news:hidtag$l2e$1@build.eclipse.org...
>> Hello,
>>
>> Im starting with a SWT app that have a view with a Tree (or text). This
>> Composite has a GridLayout with 1 column.
>> By default the control (three or text the 2 that im using now) is shown
>> as small *default size (thats ok)..
>>
>> I noticed that i can apply a GridData in the Widget (since the parent has
>> a GridLayout) that instruct the layout manager (GridLayout) how the
>> widget must the displayed.
>> But reading the help and SWT code appear some doubts....
>>
>> 1 - Where is the help or tutorials that are telling the the LayoutManager
>> cascades through the children..? Like if the composite has a Layout
>> (GridLayout) i configure the children using a GridData? :) Quite similar
>> with CSS. But default the IPageLayout is a FillLayout ... ?
>>
>> 2 - Who is the receiver that is mentioned in the SWT source code ? the
>> Display , the Widget , what ?
>>
>> 3 - Why the name of the field in GridLayout is like widthHint (that if
>> the developer put bigger than parent client area will not obey the
>> margins so is not a "hint") and in FillLayout if im not wrong is width..
>> ?
>>
>> 4 - Which is the best approach to anchor controls ?
>>
>> 5 - What does it means the true / false in the layout methods ? like
>> layout (true), layout (false) .... since i dont know what is the receiver
>> .... :)...
>>
>> Regards.
>>
>> //Snippet for layout inhiterance.
>>
>> package br.com.octech.jnewsreader.views;
>>
>> import org.eclipse.swt.SWT;
>> import org.eclipse.swt.graphics.Color;
>> import org.eclipse.swt.graphics.RGB;
>> import org.eclipse.swt.layout.GridData;
>> import org.eclipse.swt.layout.GridLayout;
>> import org.eclipse.swt.widgets.Composite;
>> import org.eclipse.swt.widgets.Event;
>> import org.eclipse.swt.widgets.Listener;
>> import org.eclipse.swt.widgets.Tree;
>> import org.eclipse.swt.widgets.TreeItem;
>> import org.eclipse.ui.part.ViewPart;
>>
>> public class JNewsServerView extends ViewPart {
>>
>> public static final String ID = "JNewsServerView.1";
>> private Composite serverPanelGroup;
>> private Tree serverTree;
>>
>> public JNewsServerView() {
>> }
>>
>> @Override
>> public void createPartControl(Composite parent) {
>> createServerPanelGroup(parent);
>> }
>>
>> private void createServerPanelGroup(Composite parent) {
>> serverPanelGroup = new Composite(parent, SWT.NONE | SWT.BORDER);
>> Color bgColor = new Color (serverPanelGroup.getDisplay(), new RGB(255 ,
>> 0 , 0));
>> serverPanelGroup.setBackground(bgColor);
>> GridLayout panelLayout = new GridLayout();
>> panelLayout.makeColumnsEqualWidth = false;
>> serverPanelGroup.setLayout(panelLayout);
>> //Here with the group if seems that grabexcess space works fine.
>> GridData panelGridData = new GridData(GridData.FILL_BOTH);
>> //Is this necessary ? or not ? in which cases i use the grabExcess....
>> fields?
>> panelGridData.grabExcessHorizontalSpace = true;
>> panelGridData.grabExcessVerticalSpace = true;
>> serverPanelGroup.setLayoutData(panelGridData);
>> serverPanelGroup.addListener(SWT.Paint, new GroupResizeListener());
>> createControls(serverPanelGroup);
>> }
>>
>> private void createControls(Composite parent) {
>> createServerTree(parent);
>> }
>>
>> private void createServerTree(Composite parent) {
>> serverTree = new Tree(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
>> serverTree.setHeaderVisible(true);
>> TreeItem topItem = new TreeItem(serverTree, SWT.NONE);
>> topItem.setText("THIS IS THE TOP ITEM");
>> serverTree.setTopItem(topItem);
>> //Since the parent is a gridlayout you can use a gridData... otherwise
>> you will receice a ClassCastException.
>> GridData treeData = new GridData(GridData.FILL_BOTH);
>> serverTree.setLayoutData(treeData);
>> //Here with the group if seems that grabexcess space works fine.
>> treeData.grabExcessHorizontalSpace = true;
>> treeData.grabExcessVerticalSpace = true;
>> }
>>
>>
>> /**
>> * @deprecated
>> */
>> private void calculateServerTreeSize() {
>> Composite parent = serverTree.getParent();
>> GridData treeGridData = null;
>> if (serverTree.getLayoutData() == null) {
>> serverTree.setLayoutData(new GridData());
>> }
>> treeGridData = (GridData) serverTree.getLayoutData();
>> treeGridData.widthHint = parent.getClientArea().width -30;
>> treeGridData.heightHint = parent.getClientArea().height -30;
>> }
>>
>> @Override
>> public void setFocus() {
>> serverTree.setFocus();
>> }
>>
>> /**
>> * @deprecated
>> * @author Administrator
>> *
>> */
>> class GroupResizeListener implements Listener
>> {
>>
>> public GroupResizeListener()
>> {}
>>
>> @Override
>> public void handleEvent(Event event) {
>> switch (event.type) {
>> case SWT.Paint:
>> Object eventSource = event.widget;
>> Composite composite = (Composite)eventSource;
>> // calculateServerTreeSize();
>> composite.layout(true , false);
>> break;
>> }
>> }
>> }
>> }
>>
>>
>
>
Previous Topic:Losing SyncExec events?
Next Topic:Listen to table item changes
Goto Forum:
  


Current Time: Sat Jul 04 06:47:58 GMT 2020

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

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

Back to the top