-1

我已将我的 CComboBox 声明如下:

     final CCombo combobox= new CCombo(shell, SWT.BORDER);
     combobox.setBounds(30, 22, 88, 21);

     ResultSet result = statement.executeQuery();

我想将类 myCombo 的对象添加到组合框

     while(result.next())
     {
          String ProName=result.getString(1);
          String ProId=result.getString(2);
          myCombo comboItem=new myCombo(ProId,ProName); //OBJECT comboItem
          combobox.addElement(comboItem); //ERROR The method addElement(myCombo)  
                                             is undefined for the type CCombo
      } 

组合框中的错误。addElement (comboItem) .... 但 addElement() 已在 CCombo 中定义。

这是类 myCombo

class myCombo{
               private String ProId;
               private String ProName;


               public myCombo(String ProId, String ProName) {
                   this.ProId=ProId;
                   this.ProName=ProName;

               }

               public String getProductName() {
                      return ProName;
                   }

               public String getProductId() {
                      return ProId;
                   }

                   @Override
               public String toString() {
                      return ProName;
               }
        }

如何取回选中的数据。
将错误显示为不能

combobox.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {



    myCombo item = (myCombo) combo.getItem(getSelectionIndex()) ; //ERROR

                    if (item!=null) {
                       System.out.printf("You've selected Product Name: %s, Product ID: %s%n", 
                             item.getProductName(), item.getProductId());
                    }

            }
        });
4

1 回答 1

2

如果你正在使用 org.eclipse.swt.custom.CCombo它没有addElement(Object o)方法。它有add(String s)你必须覆盖 toString()的方法。

      myCombo comboItem=new myCombo(ProId,ProName); 
      combobox.add(comboItem.toString())

例如

           @Override
           public String toString() {
                  return ProId+":"+ProName;
           }

获取选择,

  combo.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            System.out.print("Selected Value-");
            System.out.print(combo.getItem(combo.getSelectionIndex()));
        }
    });
于 2014-07-10T13:26:10.897 回答