如何增加jtable的数据(200分)

  • 主题发起人 主题发起人 tankhero
  • 开始时间 开始时间
T

tankhero

Unregistered / Unconfirmed
GUEST, unregistred user!
我用JTABLE类做了一个表格。其数据是一个数组。
object data = {{"as","df"},
{"ad","dfd"},
{"dfa","dfd"},
{"dfdf","dfdf"}}
如何增加一行数据,然后刷新表
 
DefaultTableModel aModel = (DefaultTableModel) jTable.getModel();
String[] ssRows = {"new record a", "new record b"} ;
aModel.addRow(ssRows) ;
jTable.setModel(aModel);
 
DefaultTableModel mTableModel = (DefaultTableModel)mMinorTb.getModel();
Object[] data = {"00","xx"};
mTableModel.addRow(data);
//增加一行数据
何为刷新表?
是指更新DB?那需要你用JDBC操作DB,与JTABLE没有直接的关系!
LUCKY!
 
呵呵!DragonPC_???快我一步
BTW: jTable.setModel(aModel);可以不要
对MODEL的操作可以刷新TABLE的显示!
不用回设MODEL
 
早起的鸟儿飞的快,我奶奶告说俺的 [:D]
 
我的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
&amp;&amp;
!(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的结束
 
一: 你定义AbstractTableModel的子类,只是负责提供JTable显示的数据,理论上只实
现三个方法即可,加上setComboboxColumn方法干吗,这是负责显示的方法,应该定义道
到编辑器和绘制器类里面。
二: 这种情况下面 public Object[][] dataTable 最好定义为 public Vector dataTable
这可以实现比较好的插入、删除功能,使用二维数组当然也可以,就不是那么太方便。
三: 插入新记录只要自己定义一个addRow(Object[] objects)方法,添加到dataTable里面
就可以了,TableModel的修改,再通过fireTableRowsInserted(int firstRow, int lastRow)
函数告诉TableModelListener们,老子加了一条记录,不就就OK了。
 
vector实现起来,有点麻烦,最好还是用OBJECT,能不能把ADDROW方法给我写一下,先谢谢您了
 
> vector实现起来,有点麻烦
有什么麻烦?
既然你钟爱数组,那就数组实现把
public void addRow(Object[] objects) {
//定义一个比现在Data数组多一行的数组,开辟内存空间来存放新的Record
Object[][] dataTemp = new Object[getRowCount() + 1][getColumnCount()];
//复制数组
System.arraycopy(dataTable, 0, dataTemp, 0, getRowCount());
//添加最后那项记录
for (int i=0;
i++;
i<getColumnCount()) {
dataTemp [getRowCount()] = objects;
}

//赋值生成的新的数组
dataTable = dataTemp ;
//产生事件通知Listener们
fireTableDataChanged() ;
//fireTableRowsInserted()也是可以的,系统开销更小

}
这些代码本着说明事实的目的,错误在所难免,最好自己在调试一下
 
后退
顶部