Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swt combobox name/key pair

Tags:

java

swt

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 in comboboxes

like image 511
zachary Avatar asked May 27 '10 15:05

zachary


1 Answers

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.

like image 91
countcb Avatar answered Sep 21 '22 00:09

countcb