【问题标题】:how to filter content in 2nd combo box according to 1st combo box selection如何根据第一个组合框选择过滤第二个组合框中的内容
【发布时间】:2018-11-07 13:49:46
【问题描述】:

我想在用户选择本科或研究生时过滤学位。我通过互联网搜索,但我无法通过示例代码找到明确的答案。

      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);
    }
}

});

这是我目前的代码,我该如何解决这个问题?

【问题讨论】:

  • 只需在顶部组合框上使用一个侦听器,对第二个框的列表进行排序......请参见此处:stackoverflow.com/questions/58939/… 和此处:stackoverflow.com/questions/17061314/… 提问时您还应该包含一些代码,显示您尝试过的内容(动作侦听器、手动排序、如何创建组合框等)。
  • 我可以根据第一个组合选择过滤该列表元素吗?
  • 是的,你可以。使用 addActionListener 找出第一个框何时更改,然后从第二个框中删除所有内容并再次将所有元素添加到其中,但顺序正确。请参阅我上面评论中的两个链接。
  • 不要使用 ==。请改用 String.equals() 方法,例如:if(s.equals("Undergraduate Degrees")) {.
  • 在添加新类别项之前清除 JComboBox:jComboBox1.removeAllItems();.

标签: java jframe jcombobox netbeans-8 ui-design


【解决方案1】:

响应您的 cmets 和更新的代码,是的,您走在正确的道路上。

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

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);
        }
    }
}

【讨论】:

  • 注意:正如DevilsHnd所说,确保你使用s.equals(而不是s ==
  • 它要求 jComboBox1 类? :(
  • @PasinduSenarath 我刚刚从您的代码中复制了jComboBox1。如果您的方法无法访问 jComboBox1,您只会看到此错误。修改您现有的代码,它应该可以工作。
  • 我的错误..我没有导入那个库。现在我修复了它。我应该在哪里初始化两个列表。因为现在我没有收到任何错误,但第二个框没有得到更新
  • @PasinduSenarath 您可以将它们存储在您想要的任何位置。这仅取决于您要授予他们什么级别的访问权限。也许只是将它们作为与动作侦听器相同的类中的私有项目。另外,如果列表没有更新,那么您确定 jComboBox1 是要更新的正确组合框吗?
【解决方案2】:

在模型中创建模型并修改数据而不是直接更新 UI 是一种很好的做法,以下是使用 DefaultComboBoxModel 的相同示例。

import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.SwingUtilities;

public class Demo{
    public static void main(String[]args){
        SwingUtilities.invokeLater(()->{
            JFrame frame = new JFrame("Dropdown Demo");
            frame.getContentPane().setLayout(new FlowLayout());
            final String SCIENCE = "Science";
            final String COMMERCE = "Commerce";
            final String SELECT = "Choose";
            frame.getContentPane().add(new JLabel("Stream"));
            JComboBox<String> streams = new JComboBox<>(new String[]{SELECT,SCIENCE,COMMERCE});
            frame.getContentPane().add(streams);
            frame.getContentPane().add(new JLabel("Subjects"));
            DefaultComboBoxModel<String> subjectsModel = new DefaultComboBoxModel<>(new String[]{SELECT});
            JComboBox<String> subjects = new JComboBox<>(subjectsModel);
            frame.getContentPane().add(subjects);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            final String[] SCIENCE_SUBJECTS = {"Maths","Biology","Physics","Chemistry"};
            final String[] COMMERCE_SUBJECTS = {"Economics","Accounts","Taxation"};
            streams.addActionListener((e)->{
                SwingUtilities.invokeLater(()->{
                    subjectsModel.removeAllElements();
                    subjectsModel.addElement(SELECT);
                    String[] temp = {};
                    if(SCIENCE.equals(streams.getSelectedItem())){
                        temp = SCIENCE_SUBJECTS;
                    } else if(COMMERCE.equals(streams.getSelectedItem())){
                        temp = COMMERCE_SUBJECTS;
                    }
                    for(String sub : temp){
                        subjectsModel.addElement(sub);
                    }
                    frame.pack();
                });
            });
        });
    }
}

上线时:

更改时:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 2014-05-24
    相关资源
    最近更新 更多