在AbstractTableModel中初始化列名

时间:2016-06-13 14:13:02

标签: java swing jtable abstracttablemodel

我想初始化我的模型。以下是我创建GUI项目的主要类的摘录:

public class IceIHM extends JFrame{

List<String> listInit = new ArrayList<String>() { 
{ add("None"); add("None"); add("None"); add("None"); add("None");add("None"); add("None"); }};

String[] tableauObjectTitle = {"Name (OBJECT)","Extended Name","Reference", "Entity Type","Relation", "Extended Relation","PMID"};
String[] tableauSubjectTitle = {"Name (SUBJECT)","Extended Name","Reference", "Entity Type","Relation", "Extended Relation","PMID"};

List<List<String>> dataObject = new ArrayList<List<String>>(){ 
{ add(listInit); add(listInit);}};

List<List<String>> dataSubject = new ArrayList<List<String>>(){ 
{ add(listInit); add(listInit);}};

ModelTableau modelObject = new ModelTableau(dataObject,tableauObjectTitle);
ModelTableau modelSubject = new ModelTableau(dataSubject, tableauSubjectTitle);

JTable tableauObject = new JTable(modelObject);
JTable tableauSubject = new JTable(modelSubject);

这是我的班级ModelTableau

public class ModelTableau extends AbstractTableModel{

private List<List<String>> data = new ArrayList<List<String>>();

private String[] headers;;

public ModelTableau(List<List<String>> data, String[] headers){
    super();
    this.data=data;
    this.headers=headers;
}

public int getRowCount() {
    return data.size();
}

public int getColumnCount() {
    return headers.length;
}

public Object getValueAt(int rowIndex, int columnIndex) {
    return data.get(rowIndex).get(columnIndex);
}

public void removeRow(int row) {
    data.remove(row);
    fireTableRowsDeleted(row, row);
}

public void addRow(List<String> listData) {
    data.add(listData);
    fireTableRowsInserted(data.size()-1, data.size()-1);

}

public void setColumnName(int i, String name) {
    headers[i] = name;
    fireTableStructureChanged();
}

}

数据已初始化,但不是我的标题。任何人都可以帮我找到我的错误吗?

1 个答案:

答案 0 :(得分:2)

覆盖getColumnName(),如here所示,并返回headers[col]

@Override
public String getColumnName(int col) {
    return headers[col];
}