I am creating an application where I want to show the courses of a bachelor education through the 3 years. Now I have a JComboBox
where you can filter the values by each year. When I select for example the 3rd year, only the courses of that year will appear.
But now I have a problem when it comes to selecting another value in the JComboBox
(for example: 1sth year) and updating the JList
. I want it to clear the list and put the new values inside of it. I can't seem to be able to make it to work. Are there some other methods to reset the 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;
}
}
}