我正在使用jtable的动态排序代码。问题是,我一输入,表就显示正确的响应,一秒钟后,它就返回到以前的原始状态。如何纠正这个问题?
private void jTextField2KeyReleased(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
TableRowSorter<TableModel> rowSorter = new TableRowSorter<>(jTable1.getModel());
jTable1.setRowSorter(rowSorter);
jTextField2.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
String text = jTextField2.getText();
if (text.trim().length() == 0) {
rowSorter.setRowFilter(null);
} else {
rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
}
}
@Override
public void removeUpdate(DocumentEvent e) {
String text = jTextField2.getText();
if (text.trim().length() == 0) {
rowSorter.setRowFilter(null);
} else {
rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
}
}
@Override
public void changedUpdate(DocumentEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
}
暂无答案!
目前还没有任何答案,快来回答吧!