-2

こんにちは、私はまだ Java の初心者です。この素晴らしい機能を学びたいと思っています... こんにちは、同じ内部と内部を持つ 4 つのコンボ ボックスがあります。

-Select-
Item 1
Item 2
Item 3
Item 4

Item 1onを選択するcomboBox1と、comboBox2,comboBox3 and comboBox4これらの要素のみが含まれます

-Select-
Item 2
Item 3
Item 4

そして、Item 3onを選択するcomboBox2と、comboBox3 and comboBox4この残りの要素があります

-Select-
Item 2
Item 4

Javaでこれを行う方法を知っている人はいますか? Netbeans で GUI ビルダーを使用しています...

編集1

これは私のコードです

private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {
    jComboBox2.removeItem(jComboBox1.getSelectedItem());
    jComboBox3.removeItem(jComboBox1.getSelectedItem());
    jComboBox4.removeItem(jComboBox1.getSelectedItem());
}

その後、同じコードを追加しjComboBox2, jComboBox3 and jComboBox4ます...私が行くときは-Select--Select-あまりにもなくなっています...そして

もう1つの問題は、すでにすべてを選択していて、もう一度交換しようと考えているときです...すべてのアイテムがなくなり、もう選択肢がありません..利用可能なアイテムをもう一度元に戻したいだけです...

編集2

jComboBox1
-Select-
Item 1
Item 2 <-- I select Item2, then the other combo box will remove Item 2**
Item 3
Item 4

jComboBox2
-Select-
Item 1
Item 3 <-- then I select Item 3
Item 4

jComboBox3
-Select-
Item 1
Item 4 <-- then Item 4

jComboBox4
-Select-
Item 1 

しかし、私は気が変わっています...そして、選択するために戻る必要がある jComboBox2ので、選択してItem3 選択 jComboBox2し、選択できる-Select-ようにしますitem3jComboBox4

しかし、結果は jComboBox4 null (アイテムなし) です

4

4 に答える 4

0

ある種の「プロキシ」モデルを使用して、個々のコンボボックスをフィルタリングできます...

さまざまなコンボ ボックスから項目を追加および削除しようとする代わりに、使用可能なすべての項目を含む単一のマスター コンボ ボックス モデルから始めることができます。

各コンボ ボックスには独自の「プロキシ」モデル (マスター モデルをベースとして使用) があり、コンボ ボックスで使用されるリストからアイテムを「フィルター」する機能があります。

そのようにすれば、「プロキシ」モデルのどの項目を除外するかを指示し、基になる API が残りを処理できるようにするだけで済みます。

于 2013-09-22T23:38:28.427 に答える
0

ループを使用すると、これらすべてのボックスとオプションをよりきれいに作成できます。このコードはテストされていませんが、動作するはずです。

//Declare and initialize the options that the comboboxes will have
String[] options = {"-Select-", "Item 1", "Item 2", "Item 3", "Item 4"};
//Declare and initialize an array that will hold the currently selected options in each combobox by index
//For example the currently selected value of comboBoxes[1] is selected[1]
String[] selected = {"-Select-", "-Select-", "-Select-", "-Select-"};

//Declare and initialize an array of comboBoxes. 
//Four comboboxes will be created all containing the options array
JComboBox[] comboBoxes = new JComboBox[4];
for(int i = 0; i < comboBox.length; i++) {
    comboBoxes[i] = new JComboBox(options);
}

private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {
    //Loop through all of the comboboxes in comboBoxes
    for(int i = 0; i < comboBoxes.length; i++) {
        //Check to see if the current combobox in the array matches the source of your event
        if(evt.getSource() == comboBoxes[i]) {
            //Get the string value of the combobox that fired the event
            String currentSelection = (String)comboBoxes[i].getSelectedItem();
            //Make sure that the value actually changed
            if(!currentSelection.equals(selected[i]) {
                //If the previous value of the combobox was "-Select-" don't add it to all the other comboboxes
                if(!selected[i].equals(options[0])) {
                    //Add back the previous value to all comboboxes other than the one that fired the event
                    for(int j = 0; j < comboBoxes.length; j++) {
                        if(j != i) {
                            comboBoxes[j].addItem(selected[i]);
                        }
                    }
                }
                //If current value of the combobox is "-Select-" don't remove it from all other comboboxes
                if(!currentSelection.equals(options[0]) {
                    //Remove the current value from all comboboxes other than the one that fired the event
                    for(int j = 0; j < comboBoxes.length; j++) {
                        if(j != i) {
                            comboBoxes[j].removeItem(comboBoxes[i].getSelectedItem());
                        }
                    }
                }
            }
            //Set the selected item for the combobox that fired the event to the current value
            selected[i] = currentSelection;
        }
    }
}
于 2013-09-22T23:59:35.520 に答える