[英]How to get row from JTable Depending on a JCombox Value Selection?
如果我單擊組合框值,那么與所選組合框具有相同值的JTable行應僅顯示。
例如:
ID Name. Category
101. Dumplings Chicken
102. Pizza Cheese
當我在組合框中選擇Chicken時,僅顯示第一行的JTable with Chicken這行。 我該怎么做呢?
使用RowFilter 。
您在TableRowSorter上安裝RowFilter:
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));
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.