1

I have two ComboBoxes each of them filters a diferent row of my JTable, what I want to do is to mantain my filter on each user select,

Example:

At the moment

First ComboBox selects Option A and table is filtered displaying only Option A

Second ComboBox select Option B and table is filtered displaying only Option B

What I need is:

First ComboBox selects Option A and table is filtered displaying matched cases for Option A

Then

Second ComboBox select Option B and table must Display values for Matching case of the first ComboBox and the second ComboBox displaying Option 'A + B'

This is my ComboBox code that filters the table individualy:

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

So is there a way to always match cases from both ComboBoxes when one option is selected?

dinotom
  • 4,990
  • 16
  • 71
  • 139
Arturo Martinez
  • 389
  • 7
  • 28

2 Answers2

2

Use RowFilter.andFilter() to allow multiple filter be applied to a single JTable with an AND logic (only if both filters are true, the item will show up) (there is also an OR, NOT,...).

Haven't tested, but I guess something like this could work:

// 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));           
    }
});
Alex S. Diaz
  • 2,637
  • 2
  • 23
  • 35
0

You can use something like this, it uses ComboBoxModel so you can add elements dynamically to your 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

                }            
            }
        }
    });

For more about adding options to JComboBox dynamically check out this post: Dynamically adding items to a JComboBox Or the DefaultComboBoxModel API here: https://docs.oracle.com/javase/7/docs/api/javax/swing/DefaultComboBoxModel.html

Hope it helps!

Community
  • 1
  • 1
adriman2
  • 21
  • 5