Grishma Dahal
Grishma Dahal

Reputation: 19

How to get row from JTable Depending on a JCombox Value Selection?

If I click on the combobox value then the JTable row with same value as the selected combobox should only get display.
For example:

ID      Name.              Category 
101.   Dumplings           Chicken
102.   Pizza               Cheese

When I select chicken in combobox, the row of JTable with Chicken that is the first row should only get displayed. How do I do this?

Upvotes: 0

Views: 31

Answers (1)

VGR
VGR

Reputation: 44414

Use a RowFilter.

You install a RowFilter on a TableRowSorter:

TableRowSorter<Dish> sorter = new TableRowSorter<>(table.getModel());
table.setRowSorter(sorter);

int categoryColumnIndex = 2;

combobox.addActionListener(e -> {
    String value = combobox.getSelectedItem().toString();
    sorter.setRowFilter(
        RowFilter.regexFilter(
            Pattern.quote(value),
            categoryColumnIndex));
});

Upvotes: 1

Related Questions