Swt组合框名称/密钥对 [英] Swt combobox name/key pair

查看:116
本文介绍了Swt组合框名称/密钥对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望文本说一件事,但是有价值说另一个

I want to have the text say one thing, but have the value say another

文字键

但它只需要一个字符串来添加项目。

But it only takes a string for adding items.

Java程序员通常如何在组合框中存储文本/ id对

How do Java programmers typically store text/id pairs in comboboxes

推荐答案

也许你可以使用组合框的setData(String key,Object value)方法来实现你想要的东西。

Maybe you can use the setData(String key, Object value) method of the combobox to achive what you want.

示例:

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"

请注意,getData方法返回一个东西。因此,您必须将其强制转换为使用setData方法设置的Type。

Note that the getData method returns an Object. So you have to cast it to the Type that you set with the setData method.

因此,您不限于将字符串设置为您的值。您可以使用setData方法将任何所需的对象设置为值。只需确保在使用getData方法再次接收数据时正确投射。

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.

编辑:
BTW,您可以在任何地方使用setData和getData方法SWT小部件。

BTW, you can use the setData and getData methods on any SWT widget.

这篇关于Swt组合框名称/密钥对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆