Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Creating own widget
Creating own widget [message #444461] Wed, 13 October 2004 14:48 Go to next message
Eclipse UserFriend
Originally posted by: roadspeed2002.yahoo.com

Hi All,
I am trying to create a widget that contains other widgets. Hence, as a test
I have created a class TwinButton that extends the Composite class and
contains a button widget. However, the text on the button does not appear.
If I create a simple button widget the text appears. What would I need to do
to make this work ? i.e are there certain methods that I have to overwrite.
thanks in advance.

public class TwinButton extends Composite {

protected Button buttonA;
// protected Button buttonB;
public TwinButton(Composite parent, int style) {
super(parent, SWT.NONE);
buttonA = new Button(this, style);

}

public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText(" Hello World ");
shell.setBounds(100, 100, 400, 250);
shell.setLayout(new FillLayout());
TwinButton button = new TwinButton(shell, SWT.CENTER );
//Button button = new Button(shell, SWT.CENTER );
button.setText("Apples");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

public void setText(String text) {
buttonA.setText(text);
}
// Methods overriding Composite
public Point computeSize (int wHint, int hHint, boolean changed) {
return buttonA.computeSize (wHint, hHint, changed);
}
/*
public void setLayout(Layout layout) {
return; // do nothing
}
*/
}
Re: Creating own widget [message #444472 is a reply to message #444461] Wed, 13 October 2004 16:05 Go to previous message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
You need to lay the button out inside the parent composite:

For example:

public class TwinButton extends Composite {
protected Button buttonA;
// protected Button buttonB;

class TwinButtonLayout extends Layout {
protected Point computeSize(Composite composite, int
wHint, int hHint, boolean flushCache) {
Button buttonA =
((TwinButton)composite).buttonA;
return buttonA.computeSize(wHint, hHint,
flushCache);
}
protected void layout(Composite composite, boolean
flushCache) {
Button buttonA =
((TwinButton)composite).buttonA;
Rectangle rect = composite.getClientArea();
buttonA.setBounds(rect);
}
}

public TwinButton(Composite parent, int style) {
super(parent, SWT.NONE);
buttonA = new Button(this, style);
super.setLayout(new TwinButtonLayout());
}
public void setText(String text) {
buttonA.setText(text);
}
// Methods overriding Composite
public Point computeSize (int wHint, int hHint, boolean
changed) {
return buttonA.computeSize (wHint, hHint, changed);
}

public void setLayout(Layout layout) {
return; // do nothing
}
}

public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText(" Hello World ");
shell.setBounds(100, 100, 400, 250);
shell.setLayout(new FillLayout());
TwinButton button = new TwinButton(shell, SWT.CENTER );
//Button button = new Button(shell, SWT.CENTER );
button.setText("Apples");
shell.setSize(100, 100);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

"Roadway" <roadspeed2002@yahoo.com> wrote in message
news:ckjf28$dvb$1@eclipse.org...
> Hi All,
> I am trying to create a widget that contains other widgets. Hence, as a
> test
> I have created a class TwinButton that extends the Composite class and
> contains a button widget. However, the text on the button does not appear.
> If I create a simple button widget the text appears. What would I need to
> do
> to make this work ? i.e are there certain methods that I have to
> overwrite.
> thanks in advance.
>
> public class TwinButton extends Composite {
>
> protected Button buttonA;
> // protected Button buttonB;
> public TwinButton(Composite parent, int style) {
> super(parent, SWT.NONE);
> buttonA = new Button(this, style);
>
> }
>
> public static void main(String[] args) {
> Display display = new Display();
> Shell shell = new Shell(display);
> shell.setText(" Hello World ");
> shell.setBounds(100, 100, 400, 250);
> shell.setLayout(new FillLayout());
> TwinButton button = new TwinButton(shell, SWT.CENTER );
> //Button button = new Button(shell, SWT.CENTER );
> button.setText("Apples");
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
> display.dispose();
> }
>
> public void setText(String text) {
> buttonA.setText(text);
> }
> // Methods overriding Composite
> public Point computeSize (int wHint, int hHint, boolean changed) {
> return buttonA.computeSize (wHint, hHint, changed);
> }
> /*
> public void setLayout(Layout layout) {
> return; // do nothing
> }
> */
> }
>
>
Previous Topic:SWT for plain COM?
Next Topic:How to capture External Key Press Events outside application window
Goto Forum:
  


Current Time: Mon Oct 26 19:52:18 GMT 2020

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

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

Back to the top