1
我正在创建一个应用程序,我想通过3年来展示学士学位课程。现在我有一个JComboBox
您可以在每年过滤值。例如,当我选择第三年时,只会显示当年的课程。当选择其他JComboBox值时清除JList
但现在我在JComboBox
(例如:1sth year)中选择另一个值并更新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;
}
}
}
'list.setModel(...)' - 如果你需要更多的帮助,然后张贴适当[MCVE]演示该问题。 – camickr