Swot combobox name / key pair

I want the text to say one thing, but it matters to say the other

Text key

But only a string is required to add items.

How Java programmers usually store text / id pairs in comboboxes

+6
java swt
source share
1 answer

Perhaps you can use the setData (String key, Object value) method for combobox to achieve 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. Therefore, you must apply it to the type specified by the setData method.

Because of this, you are not limited to setting strings as your values. You can set any object you want as a value using the setData method. Just make sure that you are doing the right thing when you get the data again using the getData method.

Edit: BTW, you can use the setData and getData methods for any SWT widget.

+13
source share

All Articles