• Post Reply Bookmark Topic Watch Topic
  • New Topic

adding objects to Combo Boxes in SWT

 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Swing, combo boxes are filled with objects. In SWT, they seem to be filled with strings. Can this be right??? Can anyone tell me how add an object instead of a string to an SWT Combo. Extra thanks if you can point me to a resource that tells how to do models and renderers for SWT Combos?

Tim
 
Shan Accent
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can add object like below

e.g.
class simpleobj {
int a;
String name;
String value;
public void setName(...){
...
}

// Add a set and get methods
...

// Override a toString() method to return the field you want disply in
// the combo box.
public String toString(){
return name;
}
}

// In combo box
JComboBox jc = new JComboBox();
simpleobj obj = new simpleobj();
obj.setName("ss");
jc.addItem(obj);
......
// You can any object like this...

bye
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
um. I'm asking about SWT, not Swing
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found the answer to this in JFace viewers:

Viewers allow you to create widgets while still using your model objects. If you use an SWT widget directly, you have to convert your objects into the strings and images expected by SWT. Viewers act as adapters on SWT widgets, handling the common code for handling widget events that you would otherwise have to implement yourself.


from:
JFace Viewers
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Example code for adding a viewer to a combo box in SWT/JFACE:



ILabelProviders are kind of like Swing Renderers
IStructuredContentProviders are kind of like Swing Models
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!