0

コンボボックス 1 (comboRole) からの選択がコンボボックス 2 (comboClass) などに入力される 3 つのコンボ ボックスで GUI をセットアップしました。2 番目のコンボボックスにデータを入力しましたが、配列内のすべての項目が 1 つの項目として扱われるため、配列内の最上位の項目のみが選択可能です。の実装方法に関係しているように感じますが、.setModelどこが間違っていたのかわかりません。

public void actionPerformed(ActionEvent a)
    {
        String selectedRole = (String) comboRole.getSelectedItem();
        if ("Tank".equals(selectedRole)){
            comboClass.removeAllItems(); //cleans out any data currently in comboClass
            comboClass.setModel(new DefaultComboBoxModel(TankClass)); //populating only top level item 
            //entire array is being treated as a single item.
        }
        else if ("Healer".equals(selectedRole)){
            comboClass.removeAllItems();
            comboClass.setModel(new DefaultComboBoxModel(HealerClass));
        } 
        else if ("Caster".equals(selectedRole)){
            comboClass.removeAllItems();
            comboClass.setModel(new DefaultComboBoxModel(CasterClass));
        }
        else if ("Damage".equals(selectedRole)){
            comboClass.removeAllItems();
            comboClass.setModel(new DefaultComboBoxModel(DpsClass));
        }
    }
4

1 に答える 1

0

問題は、コンボボックスが分割されるたびに ActionListener が実行され、コンボボックスに配列の複数のインスタンスが作成されることです。解決策は、各コンボボックスに ItemListener を使用して、そのボックスでアイテムが選択された後にのみ必要な配列を追加することです。また、ItemListener を使用するcomboClass.removeAllItems場合は、選択が変更されたときに配列を消去する必要があります。それ以外の場合は、モデルを変更するだけで、前の配列が新しい配列の上にスタックされる傾向があります。

        ItemListener roleListener = new ItemListener()
    {
        @Override
        public void itemStateChanged(ItemEvent e)                    
           {

               String selectedRole = (String) comboRole.getSelectedItem();


                if (e.getStateChange() == ItemEvent.SELECTED)
                        {
                            if ("Tank".equals(selectedRole))
                            {
                                comboClass.removeAllItems(); // removes previous selection from combobox
                                comboClass.setModel(new DefaultComboBoxModel(TankClass)); //populates comboClass with Selection

                            }
                            else if ("Healer".equals(selectedRole))
                            {
                                comboClass.removeAllItems();
                                comboClass.setModel(new DefaultComboBoxModel(HealerClass));
                            } 
                            else if ("Caster".equals(selectedRole))
                            {                
                                comboClass.removeAllItems();
                                comboClass.setModel(new DefaultComboBoxModel(CasterClass));
                            }
                            else if ("Damage".equals(selectedRole))
                            {                
                                comboClass.removeAllItems();
                                comboClass.setModel(new DefaultComboBoxModel(DpsClass));
                            }


                        }
           }

    };
于 2012-07-15T16:27:29.320 に答える