有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java绑定两个JComboBox过滤器

我有两个组合框,每个组合框过滤我的JTable的不同行,我想做的是在每个用户选择上保留我的过滤器

示例

此刻

第一个组合框选择选项A,表格被过滤,只显示选项A

第二个组合框选择选项B,表格被过滤,只显示选项B

我需要的是:

第一个组合框选择选项A,并过滤表格,显示选项A的匹配案例

然后

第二个组合框选择选项B,表格必须显示第一个组合框和显示选项“A+B”的第二个组合框的匹配大小写的值

这是我的组合框代码,用于单独筛选表:

comboBox.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent event) {
        RowFilter<DefaultTableModel, Object> rf  = RowFilter.regexFilter(comboBox.getSelectedItem().toString(), 2);
        sorter.setRowFilter(rf);
    }
});

comboBox_1.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent event) {
        RowFilter<DefaultTableModel, Object> rf  = RowFilter.regexFilter(comboBox_1.getSelectedItem().toString(), 3);
        sorter.setRowFilter(rf);                
    }
});

那么,当选择一个选项时,是否有方法始终匹配两个组合框中的案例


共 (2) 个答案

  1. # 1 楼答案

    您可以使用类似的方法,它使用ComboxModel,这样您就可以将元素动态添加到JComboBox中:

    Integer[] optionsForA = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; 
    JComboBox comboBoxA = new JComboBox(optionsForA);//A
    Vector comboItems = newVector();
    comboItems.add("A");
    comboItems.add("B");
    comboItems.add("C");
    comboItems.add("D");
    comboItems.add("E");
    final DefaultComboBoxModel mod = new DefaultComboBoxModel(comboItems);
    JComboBox comboBoxB = new JComboBox(mod);//B
    
    actionsCB.addActionListener(new ActionListener() {
    
            public void actionPerformed(ActionEvent e) {
                if()//your condition { 
    
                    for (int i = numbers.lenght; i < numbers.length + mod.size() ; i++) {
                        mod.addElement(optionsForA[i]); //add options from A to B
    
                    }            
                }
            }
        });
    

    有关向JComboBox动态添加选项的更多信息,请查看此帖子:Dynamically adding items to a JComboBox 或者这里的DefaultComboxModelAPI:https://docs.oracle.com/javase/7/docs/api/javax/swing/DefaultComboBoxModel.html

    希望有帮助

  2. # 2 楼答案

    使用RowFilter.andFilter()允许使用AND逻辑对单个JTable应用多个筛选器(仅当两个筛选器都为true时,该项才会显示)(还有一个ORNOT,…)

    我还没有测试过,但我想类似的方法可能会奏效:

    // Collection of filters to be applied to your table
    List<RowFilter<DefaultTableModel, Object>> filters = new ArrayList<>();
    
    comboBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            if(filters.isEmpty())
                filters.add(RowFilter.regexFilter(comboBox.getSelectedItem().toString(), 2));
            else
                filters.set(0, RowFilter.regexFilter(comboBox.getSelectedItem().toString(), 2));
            // Apply filters
            sorter.setRowFilter(RowFilter.andFilter(filters));
        }
    });
    
    comboBox_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            if(filters.size() < 2)
                filters.add(RowFilter.regexFilter(comboBox_1.getSelectedItem().toString(), 3));
            else
                filters.set(1, RowFilter.regexFilter(comboBox_1.getSelectedItem().toString(), 3));
            // Apply filters
            sorter.setRowFilter(RowFilter.andFilter(filters));           
        }
    });