简体   繁体   English

如何在Java中控制多个JComboBoxes?

[英]how to control multiple JComboBoxes in java?

I have trouble with setting up 12 comboboxes on a JDialog. 我在JDialog上设置12个组合框时遇到麻烦。 The first 4 are the gender of 4 individuals. 前四个是4个人的性别。 The second 4 are their personality types. 后四个是他们的性格类型。 The third 4 are their job titles. 第三四个是他们的职位。

CrewEditor

How come when I change the 1st person's gender combobox, the 2nd, 3rd, 4th gender comboboxes are all being changed at the same time? 当我更改第一人的性别组合框时,为什么第二,第三,第四性别组合框都被同时更改呢?

Why are they "linked" together? 为什么它们“链接”在一起? I already have separate ActionListeners for each? 我已经为每个人设置了单独的ActionListeners?

The same thing with the personality comboboxes and the job comboboxes. 个性组合框和工作组合框也是一样。

See attached pic. 见附图。

What do I need to do to severe whatever linking them? 我需要做些什么来严厉地将它们联系起来?

Also, Eclipse forces me to use "final" keyword on my customized JComboBoxMW. 另外,Eclipse强迫我在定制的JComboBoxMW上使用“ final”关键字。

Note that JComboBoxMW is nothing fancy. 注意,JComboBoxMW没什么特别的。 just extending the JComboBox class with better mouse wheel control. 只是通过更好的鼠标滚轮控制扩展了JComboBox类。

See a portion of code below: 请参阅下面的部分代码:

   public void setUpCrewGender() {

    List<String> genderList = new ArrayList<String>(2);
    genderList.add("M");
    genderList.add("F");
    genderComboBoxModel = new DefaultComboBoxModel<String>();
    Iterator<String> i = genderList.iterator();

    while (i.hasNext()) {
        String s = i.next();
        genderComboBoxModel.addElement(s);
    }



    final JComboBoxMW<String> g1 = new JComboBoxMW<String>(genderComboBoxModel);
    g1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e1) {
            String s1 = (String) g1.getSelectedItem();
            g1.setSelectedItem(s1);
        }});  
    g1.setMaximumRowCount(2);
    listPane.add(g1);


    final JComboBoxMW<String> g2 = new JComboBoxMW<String>(genderComboBoxModel);
    g2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e2) {
            String s2 = (String) g2.getSelectedItem();
            g2.setSelectedItem(s2);
        }});  
    g2.setMaximumRowCount(2);   
    listPane.add(g2);       

    final JComboBoxMW<String> g3 = new JComboBoxMW<String>(genderComboBoxModel);
    g3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e3) {
            String s3 = (String) g3.getSelectedItem();
            g3.setSelectedItem(s3);
        }});  
    g3.setMaximumRowCount(2);
    listPane.add(g3);



    final JComboBoxMW<String> g4 = new JComboBoxMW<String>(genderComboBoxModel);
    g4.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e4) {
            String s4 = (String) g4.getSelectedItem();
            g4.setSelectedItem(s4);
        }});  
    g4.setMaximumRowCount(2);
    listPane.add(g4);

   }

All the JComboBoxes share the same model, ie, genderComboBoxModel , and thus will all the views will show the same model-state. 所有JComboBoxes共享相同的模型,即genderComboBoxModel ,因此所有视图将显示相同的模型状态。 Solution: give them unique models. 解决方案:给他们独特的模型。

As an aside, consider using a JTable with JComboBox editors for your data entry. 顺便说一句,考虑将带有JComboBox编辑器的JTable用于数据输入。

You're using the same ComboBoxModel for each JComboBox , but the ComboBoxModel also manages the selected item, so when one combo box updates, all the other combo boxes are updated. 您为每个JComboBox使用相同的ComboBoxModel ,但是ComboBoxModel也管理选定的项目,因此,当一个组合框更新时,所有其他组合框也会更新。

You need to create a separate/individual ComboBoxModel for each combo box which contains that data you want displayed instead 您需要为每个包含要显示的数据的组合框创建一个单独的/单独的ComboBoxModel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM