如果JComboBox中有相同项目值,则不论选择相同项中某一项,结果得到的(getSelectedIndex)索引值都是该相同项的第一个出现的值对应的索引~
有没有办法使值跟索引完全对应(相同项目值,这种情况考虑在内),即调用getSelectedIndex方法,便一定得到对应的(正确的索引)

解决方案 »

  1.   

    看看API:
    int getSelectedIndex()      返回列表中与给定项匹配的第一个选项。 
    Object getSelectedItem()    返回当前所选项。你应该用这个:Object getItemAt(int index)  返回指定索引处的列表项。 
      

  2.   

    大家可能不懂我的意思,下面是例子:
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.ComboBoxModel;
    import javax.swing.DefaultComboBoxModel;
    import javax.swing.JComboBox;import javax.swing.WindowConstants;
    /**
    * This code was edited or generated using CloudGarden's Jigloo
    * SWT/Swing GUI Builder, which is free for non-commercial
    * use. If Jigloo is being used commercially (ie, by a corporation,
    * company or business for any purpose whatever) then you
    * should purchase a license for each developer using Jigloo.
    * Please visit www.cloudgarden.com for details.
    * Use of Jigloo implies acceptance of these licensing terms.
    * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
    * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
    * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
    */
    public class NewJFrame extends javax.swing.JFrame {
    private JComboBox jComboBox1; /**
    * Auto-generated main method to display this JFrame
    */
    public static void main(String[] args) {
    NewJFrame inst = new NewJFrame();
    inst.setVisible(true);
    }

    public NewJFrame() {
    super();
    initGUI();
    }

    private void initGUI() {
    try {
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    getContentPane().setLayout(null);
    {
    ComboBoxModel jComboBox1Model = new DefaultComboBoxModel(
    new String[] { "一", "一","一" });
    jComboBox1 = new JComboBox();
    getContentPane().add(jComboBox1);
    jComboBox1.setModel(jComboBox1Model);
    jComboBox1.setBounds(0, 0, 217, 42);
    jComboBox1.setFont(new java.awt.Font("宋体",0,12));
    jComboBox1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
    jComboBox1ActionPerformed(evt);
    }
    });
    }
    pack();
    setSize(400, 300);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    private void jComboBox1ActionPerformed(ActionEvent evt) {
    System.out.println(jComboBox1.getSelectedIndex());
    }}每次选择不同的"一",总是打印出索引0;
    就是说能不能解决这种情况,要的是方法
    换句话说,如何将JComboBox类进行改写?^^^^^
      

  3.   

    都没有人在吗,这是个很值得探讨的问题啊~
    我看过JComboBox类,但没有找到解决的办法,期望高手来解决~~
      

  4.   

    你可以在model里塞入对象类型
    例如:新建一个类表示下拉框中的选项:CustomItem
    public class CustomItem {
        public CustomItem(int key,String value){
            this.key = key;
            this.value = value;
        }
        private int key;
        private String value;
        public int getKey() {return key;}
        public String getValue() {return value;}
        public String toString() {return value;}
    }
     ComboBoxModel jComboBox1Model = new DefaultComboBoxModel(new Object[]{new CustomItem(0,"一"),new CustomItem(1,"一"),new CustomItem(2,"一")})
    这样显示的时候显示的是一样的值,但是里面的下拉框中的选项却是不同的对象,就可以了吧
      

  5.   

    上面已经说了可以自定义Model,7楼也给出了大致的意思。注意:对自定义对象要写equals方法,和toString方法
      

  6.   

    public class TestC extends JFrame {
    private JComboBox jComboBox1; /**
     * Auto-generated main method to display this JFrame
     */
    public static void main(String[] args) {
    TestC inst = new TestC();
    inst.setVisible(true);
    } public TestC() {
    super();
    initGUI();
    } private void initGUI() {
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    getContentPane().setLayout(null);
    {
    ComboBoxModel jComboBox1Model = new DefaultComboBoxModel(
    new Object[] { new MyComboItem("一", 0),
    new MyComboItem("一", 1), new MyComboItem("一", 2) });
    jComboBox1 = new JComboBox();
    getContentPane().add(jComboBox1);
    jComboBox1.setModel(jComboBox1Model);
    jComboBox1.setBounds(0, 0, 217, 42);
    jComboBox1.setFont(new java.awt.Font("宋体", 0, 12));
    jComboBox1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
    jComboBox1ActionPerformed(evt);
    }
    });
    }
    pack();
    setSize(400, 300);
    } private void jComboBox1ActionPerformed(ActionEvent evt) {
    System.out.println(jComboBox1.getSelectedIndex());
    } private class MyComboItem {
    private String showS = null;
    private Object value = null; protected MyComboItem(String showS, Object value) {
    this.showS = showS;
    this.value = value;
    } public Object getValue() {
    return value;
    } @Override
    public boolean equals(Object obj) {
    return value.equals(((MyComboItem) obj).getValue());
    }
    //这里只是简单的判断了一下,需要根据情况严谨判断 @Override
    public String toString() {
    return showS;
    }
    }
    }
      

  7.   

    嗯,确实能解决我的问题~
    我想要的效果是:能够重新编写JComboBox类,
    不用我们自己写代码,但实现上面出现的问题~~