JTable filtering with JTextField doesn't work
I have a JTable (DefaultTableModel) and a JTextField. I'd like to filter the JTable with the regex I put into the text field. When I start the program, all entries are shown, but when I enter text into the text field, no rows are displayed even though it should find the text within a row.
private void createFilter() {
_sorter = new TableRowSorter<DefaultTableModel>(new DefaultTableModel());
JPanel filterPanel = new JPanel();
filterPanel.setLayout(new BorderLayout());
JLabel filterLabel = new JLabel("Filter:");
filterPanel.add(filterLabel, BorderLayout.WEST);
_inputField = new JTextField();
_inputField.setColumns(40);
filterPanel.add(_inputField, BorderLayout.CENTER);
_inputField.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent de) {
newFilter();
}
public void removeUpdate(DocumentEvent de) {
newFilter();
}
public void changedUpdate(DocumentEvent de) {
newFilter();
}
});
JButton clearButton = new JButton("X");
clearButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_inputField.setText("");
}
});
filterPanel.add(clearButton, BorderLayout.EAST);
开发者_Go百科_mainFrame.add(filterPanel, BorderLayout.SOUTH);
}
private void newFilter() {
RowFilter<DefaultTableModel, Object> rowFilter = null;
try {
rowFilter = RowFilter.regexFilter(_inputField.getText());
}
catch(java.util.regex.PatternSyntaxException ex) {
return;
}
_sorter.setRowFilter(rowFilter);
_table.setRowSorter(_sorter);
}
My debugger shows me, that rowFilter is initialized, so it can't be because of a wrong RegEx. Also newFilter() is called at every keystroke.
Thanks in advance. I'd be happy to provide more information if needed.
Sincerely, Michael
It seems that the JTable and the TableRowSorter each have a different table model. The TableRowSorter should be constructed with the table model of the JTable.
At first it's easier to print stack trace on this block instead of debugging to be able to know if there was an error during initializing RowFilter.
catch(java.util.regex.PatternSyntaxException ex) {
ex.printStackTrace();
return;
}
I cannot find anything wrong with the code, it seems like problem is on the regular expression. So if you could test the regex typed into the JTextField against one of the row you expect to show to see if it matches or not: Javascript Regular Expression Validator
JTable tutorial contains example for JTable Filtering and Sorting, another examples here,
for Case Insensitive you have to set
TableRowSorter<TableModel>#setRowFilter(
RowFilter.regexFilter("(?i)" + myTextField.getText()));
EDIT:
basic workaround:
final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(tableFxModel);
myTable.setRowSorter(sorter);
filterFxText.getDocument().addDocumentListener(new DocumentListener() {
private void searchFieldChangedUpdate(DocumentEvent evt) {
String text = filterFxText.getText();
if (text.length() == 0) {
sorter.setRowFilter(null);
} else {
try {
sorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
} catch (PatternSyntaxException pse) {
JOptionPane.showMessageDialog(null, "Bad regex pattern",
"Bad regex pattern", JOptionPane.ERROR_MESSAGE);
}
}
}
@Override
public void insertUpdate(DocumentEvent evt) {
searchFieldChangedUpdate(evt);
}
@Override
public void removeUpdate(DocumentEvent evt) {
searchFieldChangedUpdate(evt);
}
@Override
public void changedUpdate(DocumentEvent evt) {
searchFieldChangedUpdate(evt);
}
});
精彩评论