更新JTable [英] Updating a JTable
问题描述
我已经多次问过我的问题,但从未见过我期望的答案.
我已经在JTable中输入了数据库的元素,并且希望能够通过某些JButton删除/添加元素.
问题是,当我添加/删除时,修改在数据库中可见,但在JTable中不可见.当我停止程序并再次运行它时,将更新JTable.
修改一行后,我该怎么办立即更新表?我试图做this.table.repaint()
,但是没有用.我想我必须对tablemodel做一些事情,可能要对fireTableStructureChanged();
做些事情,但是我不太了解如何使用它
非常感谢您的宝贵时间.
I've seen my question asked several times but I never saw the answer I expected.
I've entered elements of a database in a JTable and I want to be able to delete/add elements via some JButtons.
The problem is that when I add/delete, the modification is visible in the database but not in the JTable. When I stop the programm and run it again, the JTable is updated.
What could I do to update the table immediatly after the modification of a row? I've tried to do this.table.repaint()
but it didn't work. I think I'll have to do something with the tablemodel, probably with fireTableStructureChanged();
but I don't undersand well how to use it
Thank you very much for your time.
这是我在JTable的控制器类中的代码的一部分,我认为这没有帮助.
Here's part of my code in the controller class of the JTable, I don't think it will help..
public void update(Observable o, Object message) {
Integer iMessage = (Integer) message;
if (iMessage == Cours.CHANGEMENT_ELEVES) {
int sizeEl = this.modele.getAllEleves().size();
if (this.modele.getAllEleves() !=null) {
Vector<String[]> data = this.modele.getAllEleves();
for (int i=0; i<sizeEl; i++) {
this.table.setValueAt(data.get(i)[0],i, 0);
this.table.setValueAt(data.get(i)[1],i,1);
this.table.setValueAt(data.get(i)[2],i,2);
}
this.table.repaint();
}
}
}
推荐答案
这不是Jtable重新绘制或重新验证的问题. 当您从表模型中添加行或删除行时,您必须触发事件fireTableRowInserted偶数或tableDatachangeEvent. 以下代码是示例:
Its not a problem of Jtable repaint or revalidate. When u add row or delete row from table model, u must fire event fireTableRowInserted even or tableDatachangeEvent. Following code is example:
class myModel extends AbstractTableModel
{
////
////
////
////
////
// Remove Row from table Model
public void removeRow(int row) {
data.removeElementAt(row);
fireTableRowsDeleted(getRowCount(), getRowCount());
}
// Add new row to table
public void addRow(Vector row) {
data.addElement(row);
fireTableRowsInserted(0, getRowCount());
}
}
这篇关于更新JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!