带行过滤器的 JTable - 如何仅迭代过滤的行
JTable with row Filter - how to iterate over filtered rows only
我有 JTable,其中行被 RowFilter 过滤(B 行被隐藏)。
如何仅遍历 可见行(而不是隐藏的 B 行)?
不幸的是,下面的示例还打印了我想跳过的 B 行。
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class JTableFilterDemo {
public static void main(String[] args) {
Object[][] data = { { "A", 5 }, { "B", 2 }, { "C", 4 }, { "D", 8 } };
String columnNames[] = { "Item", "Value" };
TableModel model = new DefaultTableModel(data, columnNames) {
public Class<?> getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
JTable table = new JTable(model);
RowFilter<Object, Object> filter = new RowFilter<Object, Object>() {
public boolean include(Entry entry) {
Integer population = (Integer) entry.getValue(1);
return population.intValue() > 3;
}
};
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(
model);
sorter.setRowFilter(filter);
table.setRowSorter(sorter);
JScrollPane scrollPane = new JScrollPane(table);
JFrame frame = new JFrame("Filtering Table");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btnNewButton = new JButton("Print values");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for(int row = 0;row < table.getModel().getRowCount();row++) {
System.out.println(table.getModel().getValueAt(row, 0));
}
}
});
frame.getContentPane().add(btnNewButton, BorderLayout.SOUTH);
frame.getContentPane().add(scrollPane);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
线条
for(int row = 0;row < table.getModel().getRowCount();row++) {
System.out.println(table.getModel().getValueAt(row, 0));
}
应替换为:
for(int row = 0;row < table.getRowCount();row++) {
System.out.println(table.getModel().getValueAt(table.convertRowIndexToModel(row), 0));
}
我有 JTable,其中行被 RowFilter 过滤(B 行被隐藏)。
如何仅遍历 可见行(而不是隐藏的 B 行)?
不幸的是,下面的示例还打印了我想跳过的 B 行。
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class JTableFilterDemo {
public static void main(String[] args) {
Object[][] data = { { "A", 5 }, { "B", 2 }, { "C", 4 }, { "D", 8 } };
String columnNames[] = { "Item", "Value" };
TableModel model = new DefaultTableModel(data, columnNames) {
public Class<?> getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
JTable table = new JTable(model);
RowFilter<Object, Object> filter = new RowFilter<Object, Object>() {
public boolean include(Entry entry) {
Integer population = (Integer) entry.getValue(1);
return population.intValue() > 3;
}
};
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(
model);
sorter.setRowFilter(filter);
table.setRowSorter(sorter);
JScrollPane scrollPane = new JScrollPane(table);
JFrame frame = new JFrame("Filtering Table");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btnNewButton = new JButton("Print values");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for(int row = 0;row < table.getModel().getRowCount();row++) {
System.out.println(table.getModel().getValueAt(row, 0));
}
}
});
frame.getContentPane().add(btnNewButton, BorderLayout.SOUTH);
frame.getContentPane().add(scrollPane);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
线条
for(int row = 0;row < table.getModel().getRowCount();row++) {
System.out.println(table.getModel().getValueAt(row, 0));
}
应替换为:
for(int row = 0;row < table.getRowCount();row++) {
System.out.println(table.getModel().getValueAt(table.convertRowIndexToModel(row), 0));
}