Java 通过自定义表模型向jtable中添加行
我已经创建了一个扩展DefaultTableModel的表模型Java 通过自定义表模型向jtable中添加行,java,swing,jtable,defaulttablemodel,Java,Swing,Jtable,Defaulttablemodel,我已经创建了一个扩展DefaultTableModel的表模型 public class TableModel extends DefaultTableModel { List<VariableDetails> data; public AttachedVariableTableModel(){ super(); this.data=Collections.synchronizedList(new ArrayList<VariableDetails>())
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);
}
}
但不能添加行。没有例外和错误。这里到底出了什么问题?我怎样才能解决这个问题?提前谢谢
super()代码>
DefaultTableModel
可以添加Object[]
或Vector
AbstractTableModel
,而不是DefaultTableModel
,必须在方法中使用适当的fireXxxXxx()
重写所有,否则视图中没有可见的内容(JTable
)请允许我向您推荐一个完整的表格模型示例,以了解其工作原理。它还使用列表作为数据。最重要的是,您需要扩展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);
}
}
这里显示的代码是您的全表模型吗?因为您没有使用
数据
变量。向该变量添加数据
无效。要更快地获得更好的帮助,请发布一篇文章。
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);
}
}
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"));