我有一张桌子,我想使用JTextField
搜索/过滤它。我有代码来做到这一点,它正在工作。
现在,我要做的是,通过JComboBox
,选择我要搜索的列并向我展示我搜索过的所有行。
这是我到目前为止的代码:
private void Filtro(String query){
DefaultTableModel modelo = (DefaultTableModel) TabelaPessoal.getModel();
TableRowSorter<DefaultTableModel> pesquisa = new TableRowSorter<DefaultTableModel>(modelo);
TabelaPessoal.setRowSorter(pesquisa);
pesquisa.setRowFilter(RowFilter.regexFilter(query));
}
private void txt_pesquisaKeyReleased(java.awt.event.KeyEvent evt) {
String query = txt_pesquisa.getText().toLowerCase();
Filtro(query);
}
我怎么能这样做?
答案 0 :(得分:0)
请尝试以下代码
public class ColumnName {
public String columnName;
public int columnIndex;
public ColumnName( String columnName, int columnIndex ) {
this.columnName = columnName;
this.columnIndex = columnIndex;
}
public String getColumnName() {
return columnName;
public String toString() {
return columnName;
}
public int getColumnIndex() {
return columnIndex;
}
}
public JComboBox createComboBox( TableModel model ) {
int count = model.getColumnCount();
Object[] items = new Object[count];
for( int i = 0 ;i < count;i++ ) {
String columName = model.getColumnName(i);
items[i] = new ColumnName( columName,i);
}
return new JComboBox(items);
}
JComboBox columnCombo = createComboBox( TabelaPessoal.getModel() );
private void Filtro(String query){
DefaultTableModel modelo = (DefaultTableModel) TabelaPessoal.getModel();
TableRowSorter<DefaultTableModel> pesquisa = new
TableRowSorter<DefaultTableModel>(modelo);
TabelaPessoal.setRowSorter(pesquisa);
ColumnName columnName = ( ColumnName ) columnCombo.getSelectedItem();
pesquisa.setRowFilter( RowFilter.regexFilter(query, columnName.getColumnIndex() ) );
}