我有一个JTable,我想在旁边的JLabel中显示可见记录数。因此,每当我对JTable进行过滤或执行任何更改可见行数的操作时,我都希望使用新记录计数更新JLabel?我会采用什么样的监听器来捕获这些事件?
答案 0 :(得分:2)
Table#getRowCount()
实际上返回表视图中的行数。因此,您可以在侦听器中调用该方法,该事件会导致行计数发生更改。不幸的是,我没有意识到任何监听器会监听表行数变化的变化
最直接的监听器(针对您的过滤器案例)您可以使用RowSorterListener
“通知RowSorter的更改”。你可以这样做:
private void addListenerToSorter(RowSorter rowSorter,
final JTable table,
final JLabel rowCountLabel) {
rowSorter.addRowSorterListener(new RowSorterListener() {
@Override
public void sorterChanged(RowSorterEvent e) {
int newRowCount = table.getRowCount();
rowCountLabel.setText("Number of view rows: " + newRowCount);
}
});
}
注意RowSorterEvent
还允许您获取上一行计数,如果您对此感兴趣。
这是一个完整的例子:
import java.awt.BorderLayout;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class TestTableSortFilter {
private JTable jTable = new JTable(createTableModel());
private JLabel rowCountLabel = new JLabel();
private TableRowSorter<TableModel> rowSorter
= new TableRowSorter<>(jTable.getModel());
private JTextField jtfFilter = new JTextField();
private JButton jbtFilter = new JButton("Filter");
public TestTableSortFilter() {
jTable.setRowSorter(rowSorter);
addListenerToSorter(rowSorter, jTable, rowCountLabel);
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JLabel("Specify a number to match:"),
BorderLayout.LINE_START);
panel.add(jtfFilter, BorderLayout.CENTER);
panel.add(rowCountLabel, BorderLayout.PAGE_END);
rowCountLabel.setText("Number of view rows: " + jTable.getRowCount());
jtfFilter.getDocument().addDocumentListener(new DocumentListener(){
@Override
public void insertUpdate(DocumentEvent e) { sortRows(); }
@Override
public void removeUpdate(DocumentEvent e) { sortRows(); }
@Override
public void changedUpdate(DocumentEvent e) {}
private void sortRows() {
String text = jtfFilter.getText();
if (text.trim().length() == 0) {
rowSorter.setRowFilter(null);
} else {
rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
}
}
});
JFrame frame = new JFrame();
frame.add(panel, BorderLayout.PAGE_END);
frame.add(new JScrollPane(jTable), BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private TableModel createTableModel() {
String[] cols = {"col1", "col2", "col3", "col4"};
DefaultTableModel model = new DefaultTableModel(cols, 0);
int value = 0;
for (int i = 0; i < 100; i++) {
Vector row = new Vector();
for (int j = 0; j < cols.length; j++) {
row.add(value);
value++;
}
model.addRow(row);
}
return model;
}
private void addListenerToSorter(RowSorter rowSorter,
final JTable table,
final JLabel rowCountLabel) {
rowSorter.addRowSorterListener(new RowSorterListener() {
@Override
public void sorterChanged(RowSorterEvent e) {
int newRowCount = table.getRowCount();
rowCountLabel.setText("Number of view rows: " + newRowCount);
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new TestTableSortFilter();
}
});
}
}