0
這裏是我的表中的一些代碼合併兩個行篩選了一個JTable
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;
}
}
);
我已經使用此代碼刪除任何空白行,但在我清空我的搜索後,這似乎停止工作。有人可以幫助解決這個問題嗎?
贊同downvoter,沒有人知道發佈代碼的東西,也沒有說明,沒有骯髒的猜測 – mKorbel 2015-04-01 10:32:27
你的第一。這裏的問題很好(關於JTable),包含說明,發佈爲SSCCE/MCVE,其他... – mKorbel 2015-04-01 10:36:34
可能的相關[示例](http://stackoverflow.com/q/17854854/230513)。 – trashgod 2015-04-01 10:53:21