5

I can't figure out something using the constructor JTable(TableModel dm).

I'm using a LinkedList to manage my data so, to display it, I extended AbstractTableModel:

public class VolumeListTableModel extends AbstractTableModel {

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


    public VolumeListTableModel(){
    }

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

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

    public Object[] getColumnNames() {
        return this.columnNames;    
    }

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

I'm doing something wrong because when I run my GUI I get column names labeled A,*B*,C.

Aberrant
  • 3,423
  • 1
  • 27
  • 38
dierre
  • 7,140
  • 12
  • 75
  • 120

4 Answers4

15

There is no method in AbstractTableModel called getColumnNames, so I believe your method is being ignored. The actual method you want to override is the getColumnName method.

Try adding this method to your VolumeListTableModel class

public String getColumnName(int column) {
    return columnNames[column];
}
Codemwnci
  • 54,176
  • 10
  • 96
  • 129
4

You need to override the getColumnName method which in your case will simply

return columnNames[column];
Ben
  • 54,723
  • 49
  • 178
  • 224
Jim
  • 22,354
  • 6
  • 52
  • 80
3

You have to Override this method :

public String getColumnName(int column)
jruillier
  • 365
  • 4
  • 16
0

AbstractTableModel does not have a getColumnNames method, but it is easy to implement:

public class VolumeListTableModel extends AbstractTableModel {

    // [...]

    public String[] getColumnNames() {

        String[] columnNames = new String[this.getColumnCount()];

        for (int i = 0, columnCount = this.getColumnCount(); i < columnCount; i++) {    
            columnNames[i] = this.getColumnName(i);
        }

        return columnNames;

    }

    // [...]

}
Luca Fagioli
  • 12,722
  • 5
  • 59
  • 57