数据更改后刷新JTable [英] Refreshing JTable when data has changed
问题描述
我在刷新JTable
时遇到问题.我从一个空的ArrayList
开始,设置完我的组合框后,我将内容加载到ArrayList
,但是JTable
对此没有反应-它仍然为空. TableModel
是否有问题?
I have a problem with refreshing a JTable
. I start with an empty ArrayList
and after setting my choice of a combo box I load content to the ArrayList
but JTable
does not react to it - it remains empty. Is it a problem with a TableModel
?
这是我的代码...
public class ProjectTableModel extends AbstractTableModel{
private static final long serialVersionUID = 1L;
private static ArrayList<String> caseList = new ArrayList<String>();
@Override
public int getColumnCount() {
return 1;
}
@Override
public int getRowCount() {
return caseList.size();
}
@Override
public Object getValueAt( int row , int col) {
getRowCount();
switch(col){
case 0:
System.out.println("mam to");
return caseList.get(row);
}
return null;
}
public void setCaseList(String[] list){
for (int i =list.length - 1; i >= 0; i--) {
caseList.add(list[i]);
}
System.out.println(getRowCount());
fireTableDataChanged();
}
public String setValueAt(int row, int col){
return null;
}
public void addTestCase(String name) throws IOException{
File newDir = new File(TestCaseMaker.currentDir,
"Przypadek testowy"+(caseList.size()+1));
newDir.createNewFile();
caseList.add(name);
fireTableDataChanged();
}
public String getColumnName(int col) {
return "Przypadki testowe";
}
}
推荐答案
您对setValueAt()
的实现不正确.它的签名错误,并且无法通知其视图.
Your implementation of setValueAt()
is incorrect. It has the wrong signature, and it fails to notify its view.
附录:我 JTable
对数据模型的任何更改均不做出反应
作为参考, EnvTableTest
是使用AbstractTableModel
的示例,该示例排除了编辑; isCellEditable()
的默认实现始终返回false
.
For reference, EnvTableTest
is an example using AbstractTableModel
that precludes editing; the default implementation of isCellEditable()
always returns false
.
@Override
public void setValueAt(Object aValue, int row, int col) {
if (col == 1) {
System.out.println("setValueAt: " + row + " " + aValue);
// update caseList here
this.fireTableCellUpdated(row, col);
}
}
在此处有一个相关示例.
这篇关于数据更改后刷新JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!