显示java JTable中的特定行

时间:2013-04-10 15:24:52

标签: java swing jtable rowfilter

我有一个Java表,TableModel.each row(element)有一个ID。我想显示具体的ID。如何过滤这些元素?选定的ID可以更改。

例如:

public class L {
int id;
String name;
String family;
}

//----

private static final String[] columnNames = { "name","family"};
private static final Class[] columnClasses = {class.String.class,String.Class};
private Vector<L> list = Vector<L>();

list.add(new L(1,"A","b"));
.
.
.
list.add(new L(100,"AB","aa");

我想显示带有此ID的元素,例如{1 39 45 55 22}。

如何过滤这些ID?

1 个答案:

答案 0 :(得分:3)

你可以使用RowFilter和TableRowSorter:

final int[] ids = new int[]{1, 39, 45, 55, 22};
RowFilter<Object, Object> filter = new RowFilter<Object, Object>() {
  public boolean include(Entry entry) {
    L currentObject = (L) (entry.getValue(0));
    for(int i=0;i<ids.length;i++){
        if(currentObject.getId()==ids[i]){
            return true;
        }
    }
    return false;
  }
};

TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
sorter.setRowFilter(filter);
yourTable.setRowSorter(sorter);

经过测试,它适用于我

希望这有帮助