Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Problem creating my own widget
Problem creating my own widget [message #455479] Mon, 16 May 2005 19:36 Go to next message
Philippe Larouche is currently offline Philippe LaroucheFriend
Messages: 14
Registered: July 2009
Junior Member
Hello guys, I'm trying to create a new widget using SWT, but I have a major
problem. I want to add a button to my widget and I want it to be as wide as
the container. No matter which layoutdata i'm using, the button is always
the same width. Can someone help me and explain to me how layouts work in a
widget? Here my widget code:


public class SlidingViewer extends Composite
{
private Vector<Button> buttons = new Vector<Button>();
private ArrayList<Viewer> viewers = new ArrayList<Viewer>();
Color background;


public SlidingViewer(Composite parent, int style)
{
super(parent, style);
background = new Color(null,0,0,255);
setBackground(background);

addListener(SWT.Dispose, new Listener()
{
public void handleEvent(Event e)
{
onDispose();
}
});


addListener(SWT.RESIZE, new Listener()
{
public void handleEvent(Event e)
{
onResize();
}
});


GridLayout layout2 = new GridLayout();
layout2.numColumns = 1;
layout2.horizontalSpacing = 0;
layout2.marginHeight =0;
layout2.marginWidth = 0;
layout2.verticalSpacing = 0;
super.setLayout(layout2);
GridData gridData2 = new GridData();
gridData2.grabExcessHorizontalSpace = true;
gridData2.grabExcessVerticalSpace = true;
gridData2.horizontalAlignment = GridData.FILL;
gridData2.verticalAlignment = GridData.FILL;
setLayoutData(gridData2);

Button surveyButton = new Button(this, SWT.TOGGLE);
surveyButton.setText("Test");
GridData gridData = new GridData();
gridData.horizontalAlignment = SWT.FILL;
gridData.heightHint = 25;
surveyButton.setLayoutData(gridData);
buttons.add(surveyButton);
}


protected void onDispose()
{
if(background != null)
background.dispose();
}

}





Thanks
Phil
Re: Problem creating my own widget [message #455551 is a reply to message #455479] Tue, 17 May 2005 07:05 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: akan.aiqa.com

Philippe Larouche wrote:
> Hello guys, I'm trying to create a new widget using SWT, but I have a major
> problem. I want to add a button to my widget and I want it to be as wide as
> the container. No matter which layoutdata i'm using, the button is always
> the same width. Can someone help me and explain to me how layouts work in a
> widget? Here my widget code:
>

You can use FillLayout (or FormLayout as I always prefer) for the widget
you create and fill the composite with the button. You can find examples
at the net.

FormData button1Data = new FormData ();
button1Data.left = new FormAttachment (0, 0);
button1Data.right = new FormAttachment (100, 0);
button1Data.top = new FormAttachment (0, 0);
button1Data.bottom = new FormAttachment (100, 0);
button1.setLayoutData (button1Data);

aiQa
Re: Problem creating my own widget [message #455552 is a reply to message #455551] Tue, 17 May 2005 07:17 Go to previous messageGo to next message
Thomas FRIOL is currently offline Thomas FRIOLFriend
Messages: 38
Registered: July 2009
Member
Have you tried this :

GridData gd = new GridData(GridData.FILL_HORIZONTAL);
myButton.addLayoutData(gd);

Thomas

aiQa a écrit :
> Philippe Larouche wrote:
>
>> Hello guys, I'm trying to create a new widget using SWT, but I have a
>> major problem. I want to add a button to my widget and I want it to
>> be as wide as the container. No matter which layoutdata i'm using,
>> the button is always the same width. Can someone help me and explain
>> to me how layouts work in a widget? Here my widget code:
>>
>
> You can use FillLayout (or FormLayout as I always prefer) for the widget
> you create and fill the composite with the button. You can find examples
> at the net.
>
> FormData button1Data = new FormData ();
> button1Data.left = new FormAttachment (0, 0);
> button1Data.right = new FormAttachment (100, 0);
> button1Data.top = new FormAttachment (0, 0);
> button1Data.bottom = new FormAttachment (100, 0);
> button1.setLayoutData (button1Data);
>
> aiQa
Re: Problem creating my own widget [message #455553 is a reply to message #455552] Tue, 17 May 2005 07:28 Go to previous messageGo to next message
Emil Crumhorn is currently offline Emil CrumhornFriend
Messages: 169
Registered: July 2009
Senior Member
Thomas is correct (but he meant setLayoutData(...)), here are the changes
using your code:

Old code:
GridData gridData = new GridData();
gridData.horizontalAlignment = SWT.FILL;
gridData.heightHint = 25;
surveyButton.setLayoutData(gridData);

New code:
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.heightHint = 25;
surveyButton.setLayoutData(gridData);

And it'll take up the entire space horizontally.

Emil

"Thomas FRIOL" <thomas@anyware-tech.com> wrote in message
news:d6c5q1$qhi$1@news.eclipse.org...
> Have you tried this :
>
> GridData gd = new GridData(GridData.FILL_HORIZONTAL);
> myButton.addLayoutData(gd);
>
> Thomas
>
> aiQa a
Re: Problem creating my own widget [message #455557 is a reply to message #455553] Tue, 17 May 2005 12:56 Go to previous message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
GridLayout layout2 = new GridLayout();
layout2.numColumns = 1;
layout2.horizontalSpacing = 0;
layout2.marginHeight =0;
layout2.marginWidth = 0;
layout2.verticalSpacing = 0;
super.setLayout(layout2);

//You should not set LayoutData on your custom widget
// Move this code to where the application uses your custom widget
// When the application uses your custom widget, it will pick a layout for
// the parent of the custom widget and set the layout data on the custom
widget
// and if they do not pick GridLayout you may cause a class cast exception
//GridData gridData2 = new GridData();
//gridData2.grabExcessHorizontalSpace = true;
//gridData2.grabExcessVerticalSpace = true;
//gridData2.horizontalAlignment = GridData.FILL;
//gridData2.verticalAlignment = GridData.FILL;
//setLayoutData(gridData2);

Button surveyButton = new Button(this, SWT.TOGGLE);
surveyButton.setText("Test");
GridData gridData = new GridData();
gridData.horizontalAlignment = SWT.FILL;
// you are missing the following line
gridData.grabExcessHorizontalSpace = true;
gridData.heightHint = 25;
surveyButton.setLayoutData(gridData);
buttons.add(surveyButton);

If you have the SWT.FILL style only, then the GridLayout will make the
column as wide as the widest preferred size of the children in the column -
since you only have one child, this is the preferred width of the button.
You need to tell it to grab any additional space in the client area of the
parent. Note that this also has the side effect that your button can be
squished down to a width of zero. You can control the minimum width of the
button using GridData.minimumWidth.

For more information see:

http://www.eclipse.org/articles/Understanding%20Layouts/Unde rstanding%20Layouts.htm

http://www.eclipse.org/articles/Article-Writing%20Your%20Own %20Widget/Writing%20Your%20Own%20Widget.htm

GridLayout snippets:

http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/plat form-swt-home/dev.html#snippets
(then scroll down to the GridLayout section)
Previous Topic:Create a Browser widget WITHOUT SWT.BORDER in it.
Next Topic:VerifyListener in a TextCellEditor... bug?
Goto Forum:
  


Current Time: Fri Apr 26 17:42:44 GMT 2024

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

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

Back to the top