我创建了一个扩展DefaultTableModel的表模型。
public class TableModel extends DefaultTableModel {
List<VariableDetails> data;
public AttachedVariableTableModel(){
super();
this.data=Collections.synchronizedList(new ArrayList<VariableDetails>());
}
//method for adding rows
public void addRow(VariableDetails varDetails){
this.data.add(varDetails);
fireTableRowsInserted(this.data.size()-1,this.data.size()-1);
}
}
我尝试在表中添加已包含数据的行。
tModel.addRow(new VariableDetails());
但无法添加行。没有例外和错误。这里到底有什么不对?我该如何解决这个问题?提前谢谢。
答案 0 :(得分:2)
为什么有super();
DefaultTableModel
可以添加Object[]
或Vector
必须覆盖AbstractTableModel
而不是DefaultTableModel
,必须覆盖所有get/set
methods,并在方法中使用正确的fireXxxXxx()
,否则不会在视图中可见(JTable
)
答案 1 :(得分:0)
我建议你一个完整的表模型示例来解释它是如何工作的。它还使用List作为数据。最重要的是您需要扩展 AbstractTableModel 以使用您自己的变量来存储数据。这是一个完整的源代码示例。
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
public class MouseClickTableModel extends AbstractTableModel {
/**
*
*/
private static final long serialVersionUID = -1807522184370383957L;
private final String[] columnNames = { "Sr", "X", "Y", "Delay (ms)",
"Comment" };
public final Class[] mColTypes = { Integer.class, Integer.class,
Integer.class, Integer.class, String.class };
private final List<MouseClick> data;
public MouseClickTableModel(){
data = new ArrayList<MouseClick>(10);
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.size();
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
final MouseClick currentRow = (MouseClick) data.get(row);
switch (col) {
case 0:
return currentRow.getSequNb();
case 1:
return currentRow.getXcoo();
case 2:
return currentRow.getXycoo();
case 3:
return currentRow.getDelay();
case 4:
return currentRow.getComment();
}
return new String();
}
public Class getColumnClass(int c) {
return mColTypes[c];
}
/*
* Don't need to implement this method unless your table's editable.
*/
public boolean isCellEditable(int row, int col) {
return false;
}
public void updateRow(Object value, int row, int col) {
}
/*
* Don't need to implement this method unless your table's data can change.
*/
@Override
public void setValueAt(Object value, int row, int col) {
MouseClick currentRow = null;
if (row >= data.size()) {
// new data
currentRow = new MouseClick();
data.add(0, currentRow);
}
// update row
else {
currentRow = (MouseClick) data.get(row);
}
switch (col) {
case 0:
currentRow.setSequNb(((Integer) value).intValue());
break;
case 1:
currentRow.setXcoo(((Integer) value).intValue());
break;
case 2:
currentRow.setXycoo(((Integer) value).intValue());
break;
case 3:
currentRow.setDelay(((Integer) value).intValue());
break;
case 4:
currentRow.setComment(((String) value).toString());
break;
}
// update
fireTableCellUpdated(row, col);
}
public MouseClick getData(int row) {
return data.get(row);
}
public void addMouseClick(MouseClick mc) {
insertMouseClick(getRowCount(), mc);
}
public void insertMouseClick(int row, MouseClick mc) {
data.add(row, mc);
fireTableRowsInserted(row, row);
}
public void removeMouseClick(int row) {
data.remove(row);
fireTableRowsDeleted(row, row);
}
}
然后您只需使用您的ui设计更新您的模型。
JTable table = new JTable(new MouseClickTableModel());
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
MouseClickTableModel model = getTable().getModel());
model.insertMouseClick(0, new MouseClick(0, Integer.valueOf(xValue), Integer.valueOf(yValue), 2000, "comment"));