开发者

Swt combobox name/key pair

开发者 https://www.devze.com 2023-01-01 11:45 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号