Swt combobox name/key pair

不问归期 提交于 2019-12-06 01:07:20

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/2922681/swt-combobox-name-key-pair

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!