Swtcombobox名称/密钥对

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

文字键

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

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

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

例:

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方法返回一个Object。 因此,您必须将其强制转换为使用setData方法设置的Type。

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

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