I want to have the text say one thing, but have the value say another
Text Key
But it only takes a string for adding items.
How do Java programmers typically store text/id pairs i开发者_运维知识库n comboboxes
Maybe you can use the setData(String key, Object value) method of the combobox to achive what you want.
Example:
Combo box = new Combo(parent, SWT.DROP_DOWN);
String s = "Item 1";
box.add(s);
box.setData(s, "Some other info or object here");
s = "Item 2";
box.add(s);
box.setData(s, "This is item two");
String value = (String)box.getData("Item 2");
// value is now "This is item two"
Note that the getData method returns an Object. So you have to cast it to the Type that you set with the setData method.
Because of this you are not limited to set Strings as your values. You can set any Object you want as the value with the setData method. Just make sure you cast correctly when you receive the data again with the getData method.
Edit: BTW, you can use the setData and getData methods on any SWT widget.
精彩评论