以下是我的表格的一些代码
AthleteID.setLocation(200,120); //Sets the location
AthleteID.setSize(150,26); //Sets the size
AthleteID.setText("Athlete ID Search:");
firstPanel.add(AthleteID); //Adds it to the panel
ForeName.setLocation(731,120); //Sets the location
ForeName.setSize(150,26); //Sets the size
ForeName.setText("Athlete Name Search:");
firstPanel.add(ForeName); //Adds it to the panel
String[] aHeaders = {"Athlete ID","Forename","Surname","On The Team"};
model = new DefaultTableModel(a.compTableData,aHeaders)
{
@Override
public boolean isCellEditable(int row, int column)
{
return false;
}
};
athTable = new JTable(model);
athTable.setAutoCreateRowSorter(true);
athTable.setRowHeight(20);
myScrollTable = new JScrollPane(athTable);
myScrollTable.setSize(1082,600);
myScrollTable.setLocation(200,145);
System.out.println("Creating compare table");
sorter = new TableRowSorter(athTable.getModel());
List sortKeys = new ArrayList();
sortKeys.add(new RowSorter.SortKey(3, SortOrder.DESCENDING));
sorter.setSortKeys(sortKeys);
////////
sorter.setRowFilter
(
new RowFilter<TableModel, Integer>()
{
@Override
public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry)
{
boolean included = true;
Object cellValue = entry.getModel().getValueAt(entry.getIdentifier(), 0);
if (cellValue == null || cellValue.toString().trim().isEmpty())
{
included = false;
}
return included;
}
}
);
/////////
sorter.setComparator(0, new AlphanumComparator());
athTable.setRowSorter(sorter);
filterAthID = new JTextField(10);
filterAthID.setSize(425,26);
filterAthID.setLocation(306,120);
filterAthID.setToolTipText("Enter Athlete ID");
firstPanel.add(filterAthID);
filterForeName = new JTextField(10);
filterForeName.setSize(425,26);
filterForeName.setLocation(857,120);
filterForeName.setToolTipText("Enter Athlete Name");
firstPanel.add(filterForeName);
btnBack.setLocation(0,120);
btnBack.setSize(200,50);
btnBack.setText("Back");
btnBack.addActionListener(this);
firstPanel.add(btnBack);
Document doc = filterAthID.getDocument();
DocumentListener listener = new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e)
{
newFilter();
}
@Override
public void removeUpdate(DocumentEvent e)
{
newFilter();
}
@Override
public void changedUpdate(DocumentEvent e)
{
newFilter();
}
};
doc.addDocumentListener(listener);
Document docb = filterForeName.getDocument();
DocumentListener listenerb = new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e)
{
newFilter();
}
@Override
public void removeUpdate(DocumentEvent e)
{
newFilter();
}
@Override
public void changedUpdate(DocumentEvent e)
{
newFilter();
}
};
docb.addDocumentListener(listenerb);
for(int i=0;i<a.nextPosition;i++)
{
System.out.println(athTable.getModel().getValueAt(i,0));
}
firstPanel.add(myScrollTable);
}
private void newFilter()
{
RowFilter rf = null;
try
{
List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(2);
filters.add(RowFilter.regexFilter("(?i)"+filterAthID.getText(), 0));
filters.add(RowFilter.regexFilter("(?i)"+filterForeName.getText(), 1));
rf = RowFilter.andFilter(filters);
}
catch (java.util.regex.PatternSyntaxException e)
{
return;
}
sorter.setRowFilter(rf);
}
问题是当我在filterAthID
或filterForeName
中搜索某些内容,然后删除我搜索的代码中出现空行的内容。
sorter.setRowFilter
(
new RowFilter<TableModel, Integer>()
{
@Override
public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry)
{
boolean included = true;
Object cellValue = entry.getModel().getValueAt(entry.getIdentifier(), 0);
if (cellValue == null || cellValue.toString().trim().isEmpty())
{
included = false;
}
return included;
}
}
);
我曾使用此代码删除任何空白行,但在我清空搜索后,这似乎停止了工作。有人可以帮忙解决这个问题吗?
答案 0 :(得分:4)
合并JTable的两个行过滤器
您可以将多个过滤器合并为一个。此示例使用&#34;和&#34; fileter,但您也可以使用&#34;或&#34;过滤器:
List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(2);
filters.add(RowFilter.regexFilter(filterText.getText(), 0));
filters.add(RowFilter.regexFilter(filterText.getText(), 1));
rf = RowFilter.andFilter(filters);
此示例基于Swing教程中的Sorting and Filtering演示。