Problem creating my own widget [message #455479] |
Mon, 16 May 2005 15:36  |
Eclipse User |
|
|
|
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 #455557 is a reply to message #455553] |
Tue, 17 May 2005 08:56  |
Eclipse User |
|
|
|
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)
|
|
|
Powered by
FUDForum. Page generated in 0.04272 seconds