Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将对象添加到CComboBox_Java_Swt_Ccombobox - Fatal编程技术网

Java 如何将对象添加到CComboBox

Java 如何将对象添加到CComboBox,java,swt,ccombobox,Java,Swt,Ccombobox,我已声明我的邮箱如下: final CCombo combobox= new CCombo(shell, SWT.BORDER); combobox.setBounds(30, 22, 88, 21); ResultSet result = statement.executeQuery(); 我想将myCombo类的对象添加到combobox while(result.next()) { String ProName=re

我已声明我的邮箱如下:

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

     ResultSet result = statement.executeQuery();
我想将myCombo类的对象添加到combobox

     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()已经在cmbo中定义

这是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());
                    }

            }
        });
如果您正在使用,则它没有
addElement(Object o)
方法。它有
add(String s)
方法,您必须重写
toString()


例如

要获取所选内容

  combo.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            System.out.print("Selected Value-");
            System.out.print(combo.getItem(combo.getSelectionIndex()));
        }
    });

如果可以的话,你应该使用JFace ComboViewer而不是SWT CCombo direcltySwing,SWT是完全不同的,你应该弄清楚你在问什么。谢谢你的帮助,我也想知道。如何取回数据。在AddSelectionListener中,我找不到如何从组合框中获取所选数据并将其转换为objectOr,您可以直接打印
System.out.println(combo.getItem(getSelectionIndex())类型new SelectionAdapter()的方法getSelectionIndex()未定义{}我应该在getSelectionIndex()方法中定义什么
           @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()));
        }
    });