我想要在一个Combo控件的下拉列表中选中某一信息项后,在另一文本框里显示这一信息项的相关信息,我用combo的
combo.addModifyListener(new ModifyListener(){
public void modifyText(ModifyEvent e){

。。
}
});
但似乎不行!
请问为什么?如何解决???

解决方案 »

  1.   

    应该用ItemListener来进行监听,代码如下:
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import javax.swing.DefaultComboBoxModel;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;public class DemoForComboBox extends JFrame { private JTextField textField;
    private JComboBox comboBox;
    /**
     * Launch the application
     * @param args
     */
    public static void main(String args[]) {
    try {
    DemoForComboBox frame = new DemoForComboBox();
    frame.setVisible(true);
    } catch (Exception e) {
    e.printStackTrace();
    }
    } /**
     * Create the frame
     */
    public DemoForComboBox() {
    super();
    setTitle("测试ComboBox");
    getContentPane().setLayout(null);
    setBounds(100, 100, 310, 178);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel mJPanel=new JPanel();
    mJPanel.setLayout(null);
    mJPanel.setBounds(0, 0, 302, 144);
    this.getContentPane().add(mJPanel); comboBox = new JComboBox();
    comboBox.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4"}));
    comboBox.addItemListener(new ItemListener() {
    public void itemStateChanged(ItemEvent arg0) {
    if(comboBox.getSelectedItem()!=null)
    {
    textField.setText(comboBox.getSelectedItem().toString());
    }
    }
    });
    comboBox.setBounds(124, 23, 151, 23);
    mJPanel.add(comboBox); textField = new JTextField();
    textField.setBounds(124, 68, 151, 20);
    mJPanel.add(textField); final JLabel label = new JLabel();
    label.setText("选择值:");
    label.setBounds(10, 27, 91, 15);
    mJPanel.add(label); final JLabel label_1 = new JLabel();
    label_1.setText("选中值");
    label_1.setBounds(10, 70, 91, 15);
    mJPanel.add(label_1);
    //
    }}
      

  2.   

    大哥,忘了和你讲了,我用的是swt中的combo控件,不存在addItemListener这方法........
      

  3.   

    试试这个例子
    package demo;import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.widgets.Combo;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Shell;public class ComboExample2 {
      Display d;  Shell s;  ComboExample2() {
        d = new Display();
        s = new Shell(d);
        s.setSize(250, 250);
        
        s.setText("A Combo Example");
        final Combo c1 = new Combo(s, SWT.READ_ONLY);
        c1.setBounds(50, 50, 150, 65);
        final Combo c2 = new Combo(s, SWT.READ_ONLY);
        c2.setBounds(50, 85, 150, 65);
        c2.setEnabled(false);
        String items[] = { "Item One", "Item Two", "Item Three", "Item Four",
            "Item Five" };
        c1.setItems(items);
        c1.addSelectionListener(new SelectionAdapter() {
          public void widgetSelected(SelectionEvent e) {
            if (c1.getText().equals("Item One")) {
              String newItems[] = { "Item One A", "Item One B",
                  "Item One C" };
              c2.setItems(newItems);
              c2.setEnabled(true);
            } else if (c1.getText().equals("Item Two")) {
              String newItems[] = { "Item Two A", "Item Two B",
                  "Item Two C" };
              c2.setItems(newItems);
              c2.setEnabled(true);
            } else {
              c2.add("Not Applicable");
              c2.setText("Not Applicable");
            }      }
        });
        s.open();
        while (!s.isDisposed()) {
          if (!d.readAndDispatch())
            d.sleep();
        }
        d.dispose();
      }  public static void main(String[] argv) {
        new ComboExample2();
      }}
      

  4.   

    如果在combo的下拉项是从数据库中临时读出来的,是未知的话,如何处理?
      

  5.   

     把你数据库里的东西查出来以后先逐项添加到一个ArrayList里,如果你要显示的文本和你要使用的值不一样,则需要写类似下面的一个自定义类class myData
    {
    //用于显示的值
    String strShow="";
    //真正要使用的值
    String strValue="";
    public  myData(String show,String value)
    {
    strShow=show;
    strValue=value;
    }
    public String getStrShow() {
    return strShow;
    }
    public void setStrShow(String strShow) {
    this.strShow = strShow;
    }
    public String getStrValue() {
    return strValue;
    }
    public void setStrValue(String strValue) {
    this.strValue = strValue;
    }
    public String toString()
    {
    return strShow;
    }
    }循环将你要显示的值存入ArrayList对象myArrayList后,
    Object tmpObj[]=myArrayList.toArray();
    c1.setItems(tmpObj);即可
      

  6.   

    我也想实现这样的,好 , 试试bsr1983的方法(bsr不是人?晕)