java - 如何根据第一个组合框选择过滤第二个组合框的内容-LMLPHP

当用户选择“本科”或“研究生”时,我想过滤学位。我通过互联网搜索,但是找不到带有示例代码的明确答案。

      private String[] itemsUndergraduate = new String[]{"Computer Science", "Software Engineering"};
    private String[] itemsPostgraduate = new String[]{"BA", "Msc"};
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
   UPselect.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
    String[] itemsUndergraduate = new String[]{"Computer Science", "Software Engineering"};
    String[] itemsPostgraduate = new String[]{"BA", "Msc"};
    String s = (String) UPselect.getSelectedItem();
    if (s.equals("Undergraduate Degrees")){
        //Assign the first list to the combobox
        jComboBox1 = new JComboBox(itemsUndergraduate);
    }
    else{
        //Assign the second list to the combobox
        jComboBox1 = new JComboBox(itemsPostgraduate);
    }
}


});

到目前为止,这是我的代码,我该如何解决?

最佳答案

为了回应您的评论和更新的代码,是的,您的做法正确。

这是一个例子。首先,我们需要两个列表,以后可以使用。

String[] itemsUndergraduate = new String[]{"Computer Science", "Software Engineering"};
String[] itemsPostgraduate = new String[]{"BA", "Msc"};


现在,当选择第一个组合框时,我们可以更改第二个组合框的内容以匹配以下列表之一:

UPselect.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e){
        String s = (String) UPselect.getSelectedItem();

        //Added this line to help you debug the code
        System.out.print("Does this bit of code ever happen??");
        System.out.print("Value of selected item is: "+s);

        if (s.equals("Undergraduate Degrees")){
            //Assign the first list to the combobox
            jComboBox1 = new JComboBox(itemsUndergraduate);
        }
        else{
            //Assign the second list to the combobox
            jComboBox1 = new JComboBox(itemsPostgraduate);
        }
    }
}

关于java - 如何根据第一个组合框选择过滤第二个组合框的内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50575329/

10-11 17:17