2017-05-25 61 views
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; 
    } 
}  
} 
+3

'list.setModel(...)' - 如果你需要更多的帮助,然后张贴适当[MCVE]演示该问题。 – camickr

回答

2

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

image

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 https://stackoverflow.com/a/44191328/230513 */ 
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()); 
    } 
}