Question

I have this class:

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 */
}

And I wrote this table model:

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;        
        }
    }
}

As you can see I have a LinkedList<Filter> that I would like too show as a simple string. How can I do that?

Was it helpful?

Solution

e.g.:

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

Note:

  • Use the interface (e.g. List) rather than the implementation (LinkedList) when declaring fields (if you don't have a good reason to do otherwise).
  • ArrayList has a better performance than LinkedList in most cases (e.g. such as access by index)
  • Also override getColumnClass
  • Keep your fields private
  • setDatalist: fireTableDataChanged
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top