我的MODEL是如下:不过没有ADDROW怎么办
public class CustomTableModel extends AbstractTableModel{
public String[] columnNames = {"序号", "代码", "含义", "SQL规则"};
public Object[][] dataTable = { {"1", "User 1","123456","no"},
{"2", "User 2","123456","no"},
{"3", "User 3","123456","no"},
};
private boolean DEBUG = true;
public CustomTableModel() {
}
public CustomTableModel(String[] columnNames1,Object[][] dataTable1) {
columnNames = columnNames1;
dataTable = dataTable1;
}
public void setCol(String[] columnNames){
columnNames = columnNames;
}
public void setRow(Object dataTable){
dataTable = dataTable;
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return dataTable.length;
}
public Object getValueAt(int row, int col) {
return dataTable[row][col];
}
/*final String[] columnNames = {"序号", "代码", "含义", "SQL规则"};
final Object[][] dataTable = { {"1", "User 1","123456","no"},
{"2", "User 2","123456","no"},
{"3", "User 3","123456","no"},
{"4", "User 4","123456","no"},
};*/
public void SetColumnNames(String[] Value) {
columnNames = Value;
}
public void SetDataTable(Object[][] dataTable) {
dataTable = dataTable;
}
private void initColumnSizes(JTable table, CustomTableModel model) {
TableColumn column = null;
Component comp = null;
int headerWidth = 0;
int cellWidth = 0;
Object[] longValues = model.longValues;
for (int i = 0;
i < 5;
i++) {
column = table.getColumnModel().getColumn(i);
try {
comp = column.getHeaderRenderer().
getTableCellRendererComponent(
null, column.getHeaderValue(),
false, false, 0, 0);
headerWidth = comp.getPreferredSize().width;
} catch (NullPointerException e) {
System.err.println("Null pointer exception!");
System.err.println(" getHeaderRenderer returns null in 1.3.");
System.err.println(" The replacement is getDefaultRenderer.");
}
comp = table.getDefaultRenderer(model.getColumnClass(i)).
getTableCellRendererComponent(
table, longValues,
false, false, 0, i);
cellWidth = comp.getPreferredSize().width;
if (true) {
System.out.println("初始化列的宽度 "
+ i + ". "
+ "标题宽度 = " + headerWidth
+ ";
列宽度 = " + cellWidth);
}
//注意: 在用 Swing 1.1 Beta 2之前, 用 setMinWidth 代替.
column.setPreferredWidth(Math.max(headerWidth, cellWidth));
}
}
public void setComboboxColumn(TableColumn ComboboxColumn) {
//Set up the editor for the combobox cells.
//JComboBox的创建
JComboBox comboBox = new JComboBox();
comboBox.addItem("char");
comboBox.addItem("date");
comboBox.addItem("float");
comboBox.addItem("integer");
comboBox.addItem("boolean");
ComboboxColumn.setCellEditor(new DefaultCellEditor(comboBox));
//下拉列表的提示
DefaultTableCellRenderer renderer =
new DefaultTableCellRenderer();
renderer.setToolTipText("类型种类");
ComboboxColumn.setCellRenderer(renderer);
//安装提示到列头.
TableCellRenderer headerRenderer = ComboboxColumn.getHeaderRenderer();
if (headerRenderer instanceof DefaultTableCellRenderer) {
((DefaultTableCellRenderer)headerRenderer).setToolTipText(
"点击列显示提示");
}
}
public final Object[] longValues = {"1", "Name 1",
"char",
Boolean.TRUE, Boolean.TRUE,new Integer(2)};
public String getColumnName(int col) {
return columnNames[col];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
if (col < 2) {
return false;
} else
{
return true;
}
}
public void setValueAt(Object value, int row, int col) {
if (dataTable[0][col] instanceof Integer
&&
!(value instanceof Integer)) {
try {
dataTable[row][col] = new Integer(value.toString());
fireTableCellUpdated(row, col);
} catch (NumberFormatException e) {
}
} else
{
dataTable[row][col] = value;
fireTableCellUpdated(row, col);
}
if (DEBUG) {
System.out.println("数据的新值:");
printDebugData();
}
}
public void setCaptionAt(String value, int Number) {
columnNames[Number] = value;
}
}
// JTABLE的结束