gpt4 book ai didi

java - 选择其他 JComboBox 值时清除 JList

转载 作者:行者123 更新时间:2023-11-30 02:31:28 24 4
gpt4 key购买 nike

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

但现在我在选择 JComboBox 中的另一个值(例如:第一年)和更新 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。在下面的示例中,组合的 ActionListener 调用 JList 上的 setModel(),以将列表的模型替换为反射(reflect)当前 的模型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());
}
}

关于java - 选择其他 JComboBox 值时清除 JList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44188545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com