1

我有这堂课:

public abstract class Directory {

    protected int id;
    protected File path;
    protected LinkedList<Filter> filters;
    protected LinkedList<File> files;
    protected int wildcard;
    public static int numCols = 3;

    /* other things */
}

我写了这个表模型:

public class DirectoryListTableModel extends AbstractTableModel {

    private static final long serialVersionUID = 1L;
    private LinkedList<Directory> datalist;
    private String[] columnNames= {"ID", "Directory", "Wildcard", "Filters"};


    public DirectoryListTableModel(){
    }

    public void setDatalist(LinkedList<Directory> temp){
        this.datalist = temp;
    }

    public void showElement(){
        fireTableRowsInserted(this.datalist.size()-2,this.datalist.size()-2);
    }

    public LinkedList<Directory> getDatalist(){
        return (LinkedList<Directory>) this.datalist.clone();
    }

    @Override
    public String getColumnName(int column) {
        return this.columnNames[column];    
    }

    @Override
    public int getColumnCount() {
        return Directory.numCols;
    }

    @Override
    public int getRowCount() {
        return this.datalist.size();
    }

    @Override
    public Object getValueAt(int row, int col) {

        Directory temp = this.datalist.get(row);

        switch(col){
        case 0:
            return temp.getId();
        case 1:
            return temp.getPath();
        case 2:
            return temp.getWildcard();
        default:
            return null;        
        }
    }
}

如您所见LinkedList<Filter>,我也想将其显示为简单的字符串。我怎样才能做到这一点?

4

1 回答 1

2

例如:

case 3:
    return temp.getFilters().toString();

笔记:

  • 声明字段时使用接口(例如 List)而不是实现(LinkedList)(如果您没有充分的理由不这样做)。
  • ArrayList 在大多数情况下(例如按索引访问)比 LinkedList 有更好的性能
  • 同时覆盖 getColumnClass
  • 将您的字段保密
  • setDatalist:fireTableDataChanged
于 2010-11-20T01:27:26.847 回答