Java 具有多个相同元素的JComboBox始终选择元素的第一个实例

Java 具有多个相同元素的JComboBox始终选择元素的第一个实例,java,swing,combobox,defaultlistmodel,Java,Swing,Combobox,Defaultlistmodel,我有6个组合框使用相同的DefaultListModel和6个元素。我使用ActionListener,这样当其中一个组合框从另一个组合框选择的列表中选择一个元素时,它们会交换选择该元素的用户。又名组合框1,它选择了元素1,选择了元素2,组合框3选择了元素2,在侦听器运行之后,组合框3将选择元素1 ActionListener abilCBListener = new ActionListener(){ @Override public void actionPerformed(

我有6个组合框使用相同的DefaultListModel和6个元素。我使用ActionListener,这样当其中一个组合框从另一个组合框选择的列表中选择一个元素时,它们会交换选择该元素的用户。又名组合框1,它选择了元素1,选择了元素2,组合框3选择了元素2,在侦听器运行之后,组合框3将选择元素1

ActionListener abilCBListener = new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent evt){
        JComboBox cb = (JComboBox)evt.getSource();
        int ind = abilCBArray.indexOf(cb);
        int num = 15;
        int dup = 7;

        if(abilCBArray.size() == 6 && abilCBBoo == true){
            abilCBBoo = false;// prevents another combobox's listener from firing
            for(int i = 0; i < abilCBArray.size();i++){
                //System.out.println("i = " + i + " index = "+abilCBArray.get(i).getSelectedIndex());
                if(i != ind){
                    num -= abilCBArray.get(i).getSelectedIndex();
                    System.out.println("i = "+ i+" num = "+ num+ " Index = "+abilCBArray.get(i).getSelectedIndex() );
                    if(abilCBArray.get(i).getSelectedIndex() == cb.getSelectedIndex()){
                        dup = i;
                    } 
                }
            }
            if(num < abilCBArray.size() && dup != 7){
                abilCBArray.get(dup).setSelectedIndex(num);
            }
        }else{
            System.out.println("Tried to run abilCBArrayChange without full array");            
        }          
        abilCBBoo = true;
    }
};
ActionListener AbilicbListener=new ActionListener(){
@凌驾
已执行的公共无效操作(操作事件evt){
JComboBox cb=(JComboBox)evt.getSource();
int ind=abilCBArray.indexOf(cb);
int num=15;
int-dup=7;
if(abilCBArray.size()=6&&abilCBBoo==true){
ABLICBBOO=false;//防止另一个组合框的侦听器触发
对于(int i=0;i

问题是,用户可以将一组随机数字放入DefaultListModel,如果其中任何数字相同,组合框将选择该数字的第一个实例的索引,这会弄乱我的ActionListener。我读到的所有内容似乎都表明,为了避免组合框选择问题,您必须使列表中的每个项目都是唯一的,而且我仍然无法使用getSelectIndex(),因为如果我这样做,它仍然会返回数字的第一个实例。

您可以调用方法getSelectedItem()。它将返回所选的值

JComboBox有一个底层ComboxModel。您可以在声明中设置它的类型

 JComboBox<String> j = new JComboBox();
您将从中获得所选字符串。如果你打电话

 j.getSelectedIndex()

您将在模型中获得所选字符串的索引,您必须将其映射回以获得所选值。因此,getSelectedItem()是正确的方法

您可以调用getSelectedItem()方法。它将返回所选的值

JComboBox有一个底层ComboxModel。您可以在声明中设置它的类型

 JComboBox<String> j = new JComboBox();
您将从中获得所选字符串。如果你打电话

 j.getSelectedIndex()
您将在模型中获得所选字符串的索引,您必须将其映射回以获得所选值。因此,getSelectedItem()是正确的方法

我有6个组合框使用相同的DefaultListModel和6个元素

如果一个组合框共享相同的模型,那么当您在一个组合框中进行选择时,所有组合框都将使用相同的选择进行更新

您需要创建6个不同的DefaultListModels,每个模型包含相同的数据

,组合框将选择编号的第一个实例的索引

问题是“选择项”存储在组合框模型中,而不是所选项的索引中

调用`getSelectedIndex()方法时,它会迭代模型中的所有项,直到找到与选定对象相等的对象

如果添加到模型中的对象实现了equals方法,那么它将始终是找到的第一个对象

因此,解决方案是创建一个要存储在模型中的自定义对象

请参阅@mKorbel提供的答案

我有6个组合框使用相同的DefaultListModel和6个元素

如果一个组合框共享相同的模型,那么当您在一个组合框中进行选择时,所有组合框都将使用相同的选择进行更新

您需要创建6个不同的DefaultListModels,每个模型包含相同的数据

,组合框将选择编号的第一个实例的索引

问题是“选择项”存储在组合框模型中,而不是所选项的索引中

调用`getSelectedIndex()方法时,它会迭代模型中的所有项,直到找到与选定对象相等的对象

如果添加到模型中的对象实现了equals方法,那么它将始终是找到的第一个对象

因此,解决方案是创建一个要存储在模型中的自定义对象

请参阅@mKorbel提供的答案。

@camickr

import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Vector;

public class TestDuplicatesItems {

    private Vector<String> vec = new Vector<String>();
    private String[] degrees = {"AAS1", "AAS2", "AAS1", "AAS1"};
    private JComboBox combo = new JComboBox(vec);
    private JComboBox combo1 = new JComboBox(degrees);
    private JTextField txt = new JTextField(10);
    private JFrame frame = new JFrame("JComboBox with Duplicates Items");
    private JPanel panel = new JPanel();

    public TestDuplicatesItems() {
        vec.add("AAS1");
        vec.add("AAS1");
        vec.add("AAS1");
        vec.add("AAS1");
        //combo.setEditable(true);
        //combo.setBackground(Color.gray);
        //combo.setForeground(Color.red);        
        //combo.setEditable(true);
        //combo1.setBackground(Color.gray);
        //combo1.setForeground(Color.red);        
        txt.setText("1");
        combo.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(combo.getSelectedIndex());
                System.out.println(combo.getSelectedItem().toString());
                txt.setText(String.valueOf(combo.getSelectedIndex()));
            }
        });
        combo1.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                if ((e.getStateChange() == ItemEvent.SELECTED)) {
                    System.out.println(combo1.getSelectedIndex());
                    System.out.println(combo1.getSelectedItem().toString());
                    txt.setText(String.valueOf(combo1.getSelectedIndex()));
                }
            }
        });
        panel.add(combo);
        panel.add(combo1);
        panel.add(txt);
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                TestDuplicatesItems tdi = new TestDuplicatesItems();
            }
        });
    }
}
@卡米克尔

import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Vector;

public class TestDuplicatesItems {

    private Vector<String> vec = new Vector<String>();
    private String[] degrees = {"AAS1", "AAS2", "AAS1", "AAS1"};
    private JComboBox combo = new JComboBox(vec);
    private JComboBox combo1 = new JComboBox(degrees);
    private JTextField txt = new JTextField(10);
    private JFrame frame = new JFrame("JComboBox with Duplicates Items");
    private JPanel panel = new JPanel();

    public TestDuplicatesItems() {
        vec.add("AAS1");
        vec.add("AAS1");
        vec.add("AAS1");
        vec.add("AAS1");
        //combo.setEditable(true);
        //combo.setBackground(Color.gray);
        //combo.setForeground(Color.red);        
        //combo.setEditable(true);
        //combo1.setBackground(Color.gray);
        //combo1.setForeground(Color.red);        
        txt.setText("1");
        combo.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(combo.getSelectedIndex());
                System.out.println(combo.getSelectedItem().toString());
                txt.setText(String.valueOf(combo.getSelectedIndex()));
            }
        });
        combo1.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                if ((e.getStateChange() == ItemEvent.SELECTED)) {
                    System.out.println(combo1.getSelectedIndex());
                    System.out.println(combo1.getSelectedItem().toString());
                    txt.setText(String.valueOf(combo1.getSelectedIndex()));
                }
            }
        });
        panel.add(combo);
        panel.add(combo1);
        panel.add(txt);
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                TestDuplicatesItems tdi = new TestDuplicatesItems();
            }
        });
    }
}

getSelectedIndex()
应该为您提供所选元素的索引
getSelectedIndex()
应该为您提供所选元素的索引现在更好了;-)现在我们整理我们的评论,一切都很好;-)现在好多了;-)现在我们整理我们的评论,一切都很好;-)好吧,它必须基于基元数组,否则永远无法正常工作,Java6的bug或特性,请查看我在这里发布的代码解释,作为答案。这是我如何绕过组合框选择相同的项目,只需1组数据@Richard,是的,1组数据和6个模型就可以了,但这不是你最初的问题所说的。这就是为什么我评论说你需要6个不同的模型。好吧,它必须基于原始数组,否则永远不会正常工作,Java6的bug或特性,请查看我在这里发布的代码解释,作为答案。这是我如何绕过组合框,只使用1组数据选择相同的项目@Richard,是的,1组数据和6个模型可以,b