选择其他 JComboBox 值时清除 JList

Clear JList when selecting other JComboBox value

我正在创建一个应用程序,我想在其中展示 3 年的学士学位教育课程。现在我有一个 JComboBox,您可以在其中按每年过滤值。当我 select 例如第 3 年时,只会出现当年的课程。

但现在我在 select 在 JComboBox 中输入另一个值(例如:第 1 年)并更新 JList 时遇到了问题。我希望它清除列表并将新值放入其中。我似乎无法让它工作。还有其他方法可以重置 JList 吗?

package studiesimulator.presentatie;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.DefaultListModel;
import studiesimulator.logica.enumeraties.*;
import studiesimulator.logica.*;

/**
* @author Lorenzo 
*/
public class GUI extends javax.swing.JFrame {
private ArrayList<Curriculum> list = new ArrayList<>();
private static final String S1 = "Sem1";
private static final String S2 = "Sem2";
private ArrayList<Curriculum> newList = new ArrayList<Curriculum>();

DefaultListModel dm = new DefaultListModel();
Curriculum v = new Curriculum();

/**
 * Creates new form GUI
 */
public GUI() {
    initComponents();
    ingevenData();

    //insert comboboxvalues
    for(Studiegroep groepen : Studiegroep.values()) {
        this.jComboBox1.addItem(groepen.name());
    }

    //insert comboboxvalues
    for(StudiegroepenICT groepenICT : StudiegroepenICT.values()) {
        this.jComboBox2.addItem(groepenICT.name());
    }
}

/**
 * add data to arraylist
 */
public void ingevenData() {
    //Fase1 common
    list.add(new Curriculum("JPW275", "Computer Architecture", Fasen.FASE1, Studiegroep.ELOICT, S1, 4));
    //Example of the inserted data
}

/**
 * Change selected value of combobox.
 * 
 * @param evt Event
 */
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    //switchcase
    switch(jComboBox1.getSelectedItem().toString()) {
        case "ELOICT":
            //clear jList???
            dm.clear();
            jList1.getModel().removeListDataListener(jComboBox1);

            //Disable Combobox2
            jComboBox2.enable(false);
            break;
        case "ICT":
            //Enable combobox2
            jComboBox2.enable(true);

            //foreach fill new arraylist
            for(Curriculum list : list) {

                //filters certain values out of the new arraylist
                if(!list.getStudiegroep().toString().equals("ELO") && !list.getStudiegroep().toString().equals("ELOICT")) {

                    //fill new arraylist 
                    newList.add(list);

                    //sorts list
                    Collections.sort(newList, Collections.reverseOrder());

                    //sets data from arraylist into jList
                    jList1.setListData(newList.toArray());
                }
            }
            break;
        case "ELO":
            jComboBox2.enable(false);
            break;
        default:
            jComboBox1.setSelectedItem(Studiegroep.ELOICT);
            break;
    }
}       
}

您可以将 here 所示的方法调整为 JComboBox。在下面的示例中,组合的 ActionListenerJList 上调用 setModel() 以将列表的模型替换为反映当前 JComboBox 选择的模型。

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/** @see  */
public class ComboListTest extends JPanel implements ActionListener, Runnable {

    private final JComboBox<String> combo = new JComboBox<>(
        new String[]{"Year 1", "Year 2", "Year 3"});
    private final JList<String> list = new JList<>();
    private List<DefaultListModel<String>> models = new ArrayList<>();

    public ComboListTest() {
        super(new GridLayout(0, 1));
        models.add(new DefaultListModel<String>());
        models.get(0).addElement("A1");
        models.get(0).addElement("A2");
        models.add(new DefaultListModel<String>());
        models.get(1).addElement("B1");
        models.get(1).addElement("B2");
        models.get(1).addElement("B3");
        models.get(1).addElement("B4");
        models.add(new DefaultListModel<String>());
        models.get(2).addElement("C1");
        models.get(2).addElement("C2");

        list.setModel(models.get(0));
        this.add(combo);
        this.add(new JScrollPane(list));
        combo.addActionListener(this);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(320, 240);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int i = combo.getSelectedIndex();
        list.setModel(models.get(i));
    }

    @Override
    public void run() {
        JFrame f = new JFrame("ComboListTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new ComboListTest());
    }
}