Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Combo box doesn't take objects?
Combo box doesn't take objects? [message #445186] Wed, 27 October 2004 16:37 Go to next message
Eclipse UserFriend
Originally posted by: ukalumni.hotmail.com

I'm working on my first SWT application, so please bear with me. In
Swing, I can populate a JComboBox with a bunch of objects such as the
following:

public class NameValue
{
private String name = "Tennessee";
private String value = "TN";
public String toString()
{
return name;
}
/*
other setters and getters not included
*/
}

When that JComboBox is displayed, the toString method is called and
Tennessee shows up, but when I get the selected item, I can call the
(not included in my example) getValue() method to return "TN".

I'm sure there's a way to do this in SWT, but since the Combo object
only takes Strings, I don't know how to do this. Can anyone point me in
the right direction?

Thanks,
--Michael
Re: Combo box doesn't take objects? [message #445215 is a reply to message #445186] Thu, 28 October 2004 13:59 Go to previous messageGo to next message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
The combo box only takes strings. Use setItems() or add() to populate it.
When an item is selected, use Combo.getText() to determine the selection and
act on it In a read-only combo box, you could use Combo.getSelectionIndex()
to map the selection into an item in an array of objects. In an editable
combo, since the user can type anything, you can't do this. Hope this
helps.

"Michael Molloy" <ukalumni@hotmail.com> wrote in message
news:cloimv$rt6$1@eclipse.org...
> I'm working on my first SWT application, so please bear with me. In
> Swing, I can populate a JComboBox with a bunch of objects such as the
> following:
>
> public class NameValue
> {
> private String name = "Tennessee";
> private String value = "TN";
> public String toString()
> {
> return name;
> }
> /*
> other setters and getters not included
> */
> }
>
> When that JComboBox is displayed, the toString method is called and
> Tennessee shows up, but when I get the selected item, I can call the
> (not included in my example) getValue() method to return "TN".
>
> I'm sure there's a way to do this in SWT, but since the Combo object
> only takes Strings, I don't know how to do this. Can anyone point me in
> the right direction?
>
> Thanks,
> --Michael
Re: Combo box doesn't take objects? [message #445231 is a reply to message #445186] Fri, 29 October 2004 11:30 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: micasim.gmx.de

On Wed, 27 Oct 2004 11:37:14 -0500, Michael Molloy <ukalumni@hotmail.com>
wrote:

> I'm working on my first SWT application, so please bear with me. In
> Swing, I can populate a JComboBox with a bunch of objects such as the
> following:
>
> public class NameValue
> {
> private String name = "Tennessee";
> private String value = "TN";
> public String toString()
> {
> return name;
> }
> /*
> other setters and getters not included
> */
> }
>
> When that JComboBox is displayed, the toString method is called and
> Tennessee shows up, but when I get the selected item, I can call the
> (not included in my example) getValue() method to return "TN".
>
> I'm sure there's a way to do this in SWT, but since the Combo object
> only takes Strings, I don't know how to do this. Can anyone point me in
> the right direction?
>
> Thanks,
> --Michael

Hi Michael,

Looking for methods you know from Swing in SWT, you'll probably fail. SWT
is an alternative to AWT. JFace sits on top of SWT and is more of the kind
of Swing.
In your example, use org.eclipse.jface.viewers.ComboViewer on top of the
swt.widgets.Combo:
ComboViewer vwr = new ComboViewer (swtCombo);
vwr.setContentProvider (new
org.eclipse.jface.viewers.ArrayContentProvider());
vwr.setLabelProvider (new org.eclipse.jface.viewers.LabelProvider());
....
vwr.setInput (list); // list may be an array or a Collection holding the
objects to display, e.g., NameValue[] list;

Hope that helps,
Michael
Re: Combo box doesn't take objects? [message #445239 is a reply to message #445231] Fri, 29 October 2004 12:30 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: ukalumni.hotmail.com

Thanks for your response, and Steve, if you're reading this, thank you
as well.

I'm trying to unlearn Swing as I go.

--Michael

Michael Simons wrote:
> On Wed, 27 Oct 2004 11:37:14 -0500, Michael Molloy
> <ukalumni@hotmail.com> wrote:
>
>> I'm working on my first SWT application, so please bear with me. In
>> Swing, I can populate a JComboBox with a bunch of objects such as the
>> following:
>>
>> public class NameValue
>> {
>> private String name = "Tennessee";
>> private String value = "TN";
>> public String toString()
>> {
>> return name;
>> }
>> /*
>> other setters and getters not included
>> */
>> }
>>
>> When that JComboBox is displayed, the toString method is called and
>> Tennessee shows up, but when I get the selected item, I can call the
>> (not included in my example) getValue() method to return "TN".
>>
>> I'm sure there's a way to do this in SWT, but since the Combo object
>> only takes Strings, I don't know how to do this. Can anyone point me
>> in the right direction?
>>
>> Thanks,
>> --Michael
>
>
> Hi Michael,
>
> Looking for methods you know from Swing in SWT, you'll probably fail.
> SWT is an alternative to AWT. JFace sits on top of SWT and is more of
> the kind of Swing.
> In your example, use org.eclipse.jface.viewers.ComboViewer on top of
> the swt.widgets.Combo:
> ComboViewer vwr = new ComboViewer (swtCombo);
> vwr.setContentProvider (new
> org.eclipse.jface.viewers.ArrayContentProvider());
> vwr.setLabelProvider (new org.eclipse.jface.viewers.LabelProvider());
> ...
> vwr.setInput (list); // list may be an array or a Collection holding
> the objects to display, e.g., NameValue[] list;
>
> Hope that helps,
> Michael
Re: Combo box doesn't take objects? [message #445254 is a reply to message #445239] Fri, 29 October 2004 17:59 Go to previous message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
In SWT, the data is front loaded into the widgets rather than implementing a
method that gets called when the data is required. Rather, interaction is
done through listeners. As of 3.0, only the virtual table requests data at
a later time. Hope this helps.

"Michael Molloy" <ukalumni@hotmail.com> wrote in message
news:cltdbs$aur$1@eclipse.org...
> Thanks for your response, and Steve, if you're reading this, thank you
> as well.
>
> I'm trying to unlearn Swing as I go.
>
> --Michael
>
> Michael Simons wrote:
> > On Wed, 27 Oct 2004 11:37:14 -0500, Michael Molloy
> > <ukalumni@hotmail.com> wrote:
> >
> >> I'm working on my first SWT application, so please bear with me. In
> >> Swing, I can populate a JComboBox with a bunch of objects such as the
> >> following:
> >>
> >> public class NameValue
> >> {
> >> private String name = "Tennessee";
> >> private String value = "TN";
> >> public String toString()
> >> {
> >> return name;
> >> }
> >> /*
> >> other setters and getters not included
> >> */
> >> }
> >>
> >> When that JComboBox is displayed, the toString method is called and
> >> Tennessee shows up, but when I get the selected item, I can call the
> >> (not included in my example) getValue() method to return "TN".
> >>
> >> I'm sure there's a way to do this in SWT, but since the Combo object
> >> only takes Strings, I don't know how to do this. Can anyone point me
> >> in the right direction?
> >>
> >> Thanks,
> >> --Michael
> >
> >
> > Hi Michael,
> >
> > Looking for methods you know from Swing in SWT, you'll probably fail.
> > SWT is an alternative to AWT. JFace sits on top of SWT and is more of
> > the kind of Swing.
> > In your example, use org.eclipse.jface.viewers.ComboViewer on top of
> > the swt.widgets.Combo:
> > ComboViewer vwr = new ComboViewer (swtCombo);
> > vwr.setContentProvider (new
> > org.eclipse.jface.viewers.ArrayContentProvider());
> > vwr.setLabelProvider (new org.eclipse.jface.viewers.LabelProvider());
> > ...
> > vwr.setInput (list); // list may be an array or a Collection holding
> > the objects to display, e.g., NameValue[] list;
> >
> > Hope that helps,
> > Michael
Previous Topic:possible to change the location of a currently added tableItem in a swt table
Next Topic:Catch KeyEvents
Goto Forum:
  


Current Time: Thu Apr 25 22:21:39 GMT 2024

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

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

Back to the top