1

こんにちは、私はまだ 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

1 に答える 1

0

Not sure which of your two answers will be deleted, but here's the same answer again. Note that you can create all of your JComboBoxes and options using loops to prevent really lengthy repetitive code. Then you can use the getSource() method to tell which combobox the event came from. If you created your JComboBoxes as an array you can loop through them very cleanly. In order to add things back in I would just keep track of what has been selected and in which combobox using a String array. You can then check this array and use it to add items back in as needed. Note that they won't go back in the same order. If you want that functionality you can play around with insertItemAt, but that would probably get a little messy (since the indices are constantly changing since you're adding and removing items) so I've left it out.

//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-23T00:04:28.997 に答える