Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Adding a control to a composite
Adding a control to a composite [message #448469] Mon, 10 January 2005 18:38 Go to next message
Philip Borlin is currently offline Philip BorlinFriend
Messages: 17
Registered: July 2009
Junior Member
I am coming from a Swing background and had a question about how to add or
remove controls on a composite at an arbitrary time. For instance in
Swing I would do the following:

private JPanel panel = new JPanel();
private Component component1;
private Component component2;

eventOccured(EventObject o) {
if (o.getBoolean()) {
panel.remove(component1);
panel.add(component2);
} else {
panel.remove(component2);
panel.add(component1);
}
}

What would the SWT equivilent look like?

Thanks,
-Phil
Re: Adding a control to a composite [message #448482 is a reply to message #448469] Tue, 11 January 2005 02:45 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: kingbow_king.yahoo.com

There is no such way you can do this in SWT. Becuase all SWT controls are
native controls and consume operating system resources, you must
explicitly dispose it as the pointer in C++.

So, since an add() method to do something reasonable, it would require
that you be able to remove a widget from the children list without
destroying it. Given that a widget cannot exist without a parent, this
would leave the child in a state where it knows about its parent but the
parent does not know about the child.

SWT does not have a widget remove() method for the same reason that there
is no add() method: It would leave the child in a state where it knows
about its parent but the parent does not know about the child. Because
widgets are alive for exactly the duration that they are referenced by
their parents, implicit finalization (as provided by the garbage
collector) does not make sense for widgets. Widgets are not finalized.

for more information about this, you can refer to:
http://www.eclipse.org/articles/swt-design-2/swt-design-2.ht ml

Philip Borlin wrote:

> I am coming from a Swing background and had a question about how to add or
> remove controls on a composite at an arbitrary time. For instance in
> Swing I would do the following:

> private JPanel panel = new JPanel();
> private Component component1;
> private Component component2;

> eventOccured(EventObject o) {
> if (o.getBoolean()) {
> panel.remove(component1);
> panel.add(component2);
> } else {
> panel.remove(component2);
> panel.add(component1);
> }
> }

> What would the SWT equivilent look like?

> Thanks,
> -Phil
Re: Adding a control to a composite [message #448492 is a reply to message #448469] Tue, 11 January 2005 14:42 Go to previous messageGo to next message
Charlie Surface is currently offline Charlie SurfaceFriend
Messages: 40
Registered: July 2009
Member
You should take a look at Control.setParent(). However, it may not be
implemented everywhere, and is generally not a good API to call.

Charlie


Philip Borlin wrote:

> I am coming from a Swing background and had a question about how to add
> or remove controls on a composite at an arbitrary time. For instance in
> Swing I would do the following:
>
> private JPanel panel = new JPanel();
> private Component component1;
> private Component component2;
>
> eventOccured(EventObject o) {
> if (o.getBoolean()) {
> panel.remove(component1);
> panel.add(component2);
> } else {
> panel.remove(component2);
> panel.add(component1);
> }
> }
>
> What would the SWT equivilent look like?
>
> Thanks,
> -Phil
>
Re: Adding a control to a composite [message #448496 is a reply to message #448482] Tue, 11 January 2005 15:01 Go to previous messageGo to next message
Philip Borlin is currently offline Philip BorlinFriend
Messages: 17
Registered: July 2009
Junior Member
OK, I didn't mean I was looking for an add() and remove() mechanism so
maybe my example was a bad one. Basically I want to create a couple of
composites and choose which one to show based on an event. An example is
on our main screen we have account information shown in a panel at the
top. When I change accounts, the application looks at the subclass of the
new account and changes the top panel to hold the correct composite.

I know this can be done because TabFolder does this. I tried to look
through the code for CTabFolder (since it is a non-native widget) but have
no idea how they get their control to change when you click a new tab.

Thanks,
-Phil

Kingbow King wrote:

> There is no such way you can do this in SWT. Becuase all SWT controls are
> native controls and consume operating system resources, you must
> explicitly dispose it as the pointer in C++.

> So, since an add() method to do something reasonable, it would require
> that you be able to remove a widget from the children list without
> destroying it. Given that a widget cannot exist without a parent, this
> would leave the child in a state where it knows about its parent but the
> parent does not know about the child.

> SWT does not have a widget remove() method for the same reason that there
> is no add() method: It would leave the child in a state where it knows
> about its parent but the parent does not know about the child. Because
> widgets are alive for exactly the duration that they are referenced by
> their parents, implicit finalization (as provided by the garbage
> collector) does not make sense for widgets. Widgets are not finalized.

> for more information about this, you can refer to:
> http://www.eclipse.org/articles/swt-design-2/swt-design-2.ht ml

> Philip Borlin wrote:

>> I am coming from a Swing background and had a question about how to add or
>> remove controls on a composite at an arbitrary time. For instance in
>> Swing I would do the following:

>> private JPanel panel = new JPanel();
>> private Component component1;
>> private Component component2;

>> eventOccured(EventObject o) {
>> if (o.getBoolean()) {
>> panel.remove(component1);
>> panel.add(component2);
>> } else {
>> panel.remove(component2);
>> panel.add(component1);
>> }
>> }

>> What would the SWT equivilent look like?

>> Thanks,
>> -Phil
Re: Adding a control to a composite [message #448528 is a reply to message #448496] Tue, 11 January 2005 16:22 Go to previous messageGo to next message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
CTabFolder uses Control.setVisible() and Control.setBounds() to manipulate
controls that have already been created.

"Philip Borlin" <pborlin@cmcflex.com> wrote in message
news:cs0po7$gbe$1@www.eclipse.org...
> OK, I didn't mean I was looking for an add() and remove() mechanism so
> maybe my example was a bad one. Basically I want to create a couple of
> composites and choose which one to show based on an event. An example is
> on our main screen we have account information shown in a panel at the
> top. When I change accounts, the application looks at the subclass of the
> new account and changes the top panel to hold the correct composite.
>
> I know this can be done because TabFolder does this. I tried to look
> through the code for CTabFolder (since it is a non-native widget) but have
> no idea how they get their control to change when you click a new tab.
>
> Thanks,
> -Phil
>
> Kingbow King wrote:
>
> > There is no such way you can do this in SWT. Becuase all SWT controls
are
> > native controls and consume operating system resources, you must
> > explicitly dispose it as the pointer in C++.
>
> > So, since an add() method to do something reasonable, it would require
> > that you be able to remove a widget from the children list without
> > destroying it. Given that a widget cannot exist without a parent, this
> > would leave the child in a state where it knows about its parent but the
> > parent does not know about the child.
>
> > SWT does not have a widget remove() method for the same reason that
there
> > is no add() method: It would leave the child in a state where it knows
> > about its parent but the parent does not know about the child. Because
> > widgets are alive for exactly the duration that they are referenced by
> > their parents, implicit finalization (as provided by the garbage
> > collector) does not make sense for widgets. Widgets are not finalized.
>
> > for more information about this, you can refer to:
> > http://www.eclipse.org/articles/swt-design-2/swt-design-2.ht ml
>
> > Philip Borlin wrote:
>
> >> I am coming from a Swing background and had a question about how to add
or
> >> remove controls on a composite at an arbitrary time. For instance in
> >> Swing I would do the following:
>
> >> private JPanel panel = new JPanel();
> >> private Component component1;
> >> private Component component2;
>
> >> eventOccured(EventObject o) {
> >> if (o.getBoolean()) {
> >> panel.remove(component1);
> >> panel.add(component2);
> >> } else {
> >> panel.remove(component2);
> >> panel.add(component1);
> >> }
> >> }
>
> >> What would the SWT equivilent look like?
>
> >> Thanks,
> >> -Phil
>
>
Re: Adding a control to a composite [message #448533 is a reply to message #448528] Tue, 11 January 2005 17:48 Go to previous messageGo to next message
Philip Borlin is currently offline Philip BorlinFriend
Messages: 17
Registered: July 2009
Junior Member
OK, that makes sense. I put together a little demo (it uses labels
instead of composites just to be simpler) and things are working well.

The interesting code looks like:
final Composite compositeContainer = new Composite(parent, SWT.NONE);
final Label label1 = new Label(compositeContainer, SWT.NONE);
label1.setText("Label 1");
final Label label2 = new Label(compositeContainer, SWT.NONE);
label2.setText("Label 2");
Button button = new Button(parent, SWT.NONE);
button.setText("Change Composite");
button.addListener(SWT.Selection, new Listener() {

public void handleEvent(Event event) {
if (condition) {
label2.setVisible(false);
label1.setBounds(compositeContainer.getBounds());
label1.setVisible(true);
} else {
label1.setVisible(false);
label2.setBounds(compositeContainer.getBounds());
label2.setVisible(true);
}

condition = !condition;
}

});

I had to set the previous component to setVisible(false) but that makes
sense.

I appreciate all of the help!

-Phil


Steve Northover wrote:

> CTabFolder uses Control.setVisible() and Control.setBounds() to manipulate
> controls that have already been created.
Re: Adding a control to a composite [message #448534 is a reply to message #448533] Tue, 11 January 2005 18:18 Go to previous message
Eclipse UserFriend
Originally posted by: wiz.vball.net

There's an easier way to do this. Simply use StackLayout and then set
the top control to which ever composite you want to be displayed. No
need for all that grunt work :-)

StackLayout stack;
stack = new StackLayout();

if( something )
stack.topControl = composite1;
else
stack.topControl = composite2;


On Tue, 11 Jan 2005 17:48:40 +0000 (UTC), pborlin@cmcflex.com (Philip
Borlin) wrote:

>OK, that makes sense. I put together a little demo (it uses labels
>instead of composites just to be simpler) and things are working well.
>
>The interesting code looks like:
> final Composite compositeContainer = new Composite(parent, SWT.NONE);
> final Label label1 = new Label(compositeContainer, SWT.NONE);
> label1.setText("Label 1");
> final Label label2 = new Label(compositeContainer, SWT.NONE);
> label2.setText("Label 2");
> Button button = new Button(parent, SWT.NONE);
> button.setText("Change Composite");
> button.addListener(SWT.Selection, new Listener() {
>
> public void handleEvent(Event event) {
> if (condition) {
> label2.setVisible(false);
> label1.setBounds(compositeContainer.getBounds());
> label1.setVisible(true);
> } else {
> label1.setVisible(false);
> label2.setBounds(compositeContainer.getBounds());
> label2.setVisible(true);
> }
>
> condition = !condition;
> }
>
> });
>
>I had to set the previous component to setVisible(false) but that makes
>sense.
>
>I appreciate all of the help!
>
>-Phil
>
>
>Steve Northover wrote:
>
>> CTabFolder uses Control.setVisible() and Control.setBounds() to manipulate
>> controls that have already been created.
Previous Topic:Add an item to Text menu?
Next Topic:NoClassDefFoundError In SWT Display Event Loop
Goto Forum:
  


Current Time: Sat Apr 20 02:14:00 GMT 2024

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

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

Back to the top