【问题标题】:Manipulate combobox using dictionary in java在java中使用字典操作组合框
【发布时间】:2013-04-24 07:27:44
【问题描述】:

我正在使用一个 Web 服务,它返回“ArrayOfKeyValueOfintstring”类型的结果

我很困惑如何在 java 中将此数据添加到我的组合框中。

这是我的代码

 org.tempuri.ThirdPartyService service = new org.tempuri.ThirdPartyService();
 org.tempuri.IThirdPartyService port = service.getBasicHttpBindingIThirdPartyService();
 // TODO initialize WS operation arguments here
 java.lang.String key = line.trim();
 // TODO process result here
 String>)port.getTests(key).getKeyValueOfintstring();
 com.microsoft.schemas._2003._10.serialization.arrays.ArrayOfKeyValueOfintstring result = port.getVulnerabilities(key);

编辑

for(int i=0;i<=result.getKeyValueOfintstring().size();i++)
{
   result.getKeyValueOfintstring().get(i).getKey();
   result.getKeyValueOfintstring().get(i).getValue();
   JOptionPane.showMessageDialog(null, "key is"+result.getKeyValueOfintstring().get(i).getKey());
   JOptionPane.showMessageDialog(null, "Value is"+result.getKeyValueOfintstring().get(i).getValue());
   model.addElement(new Item(key, value));

 }

我尝试在对话框中获取密钥对,但我得到了正确的结果。但现在我不知道如何将它们添加到我的 ComboBox 中。我创建了表“向量模型 = 新向量();”并将其添加到组合框中,例如“cbTestName = new JComboBox(model);”

这是正确的方法还是我需要应用其他任何东西来将键值对添加到我的组合框。

【问题讨论】:

    标签: java wcf web-services dictionary combobox


    【解决方案1】:

    如果你要去声明

    port.getTests(key).getKeyValueOfintstring(),

    你可能会发现它被实现为

    List<KeyValuePairOfintstring>
    

    KeyValuePairOfintstring 看起来像

    ...
    
        protected Integer key;
    ...  
        protected String value;
    

    因此,您可以做的其中一种方法是在循环中运行 port.getTests(key).getKeyValueOfintstring(),并使用您希望在组合框中显示的 java 业务对象构建您的地图。

    您可以覆盖对象的 toString 方法,这是控制它们在 ComboBox 中的外观的最简单方法。

    【讨论】:

    • 这里我作为参数传递的密钥是我的身份验证密钥,而不是来自“密钥值”对的那个
    • 您是否查看了 getKeyValueOfintstring() 声明?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多