简体   繁体   English

数据更改时刷新JTable

[英]Refreshing JTable when data has changed

I have a problem with refreshing a JTable . 刷新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. 我从一个空的ArrayList开始,在设置我选择的一个组合框后,我将内容加载到ArrayListJTable没有对它作出反应 - 它仍然是空的。 Is it a problem with a TableModel ? 这是TableModel的问题吗?

This is my code... 这是我的代码......

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";
    }
}

Your implementation of setValueAt() is incorrect. 您的setValueAt()实现不正确。 It has the wrong signature, and it fails to notify its view. 它具有错误的签名,并且无法通知其视图。

Addendum: My JTable does not react to any changes to data model 附录: 我的 JTable 对数据模型的任何更改都没有反应

For reference, EnvTableTest is an example using AbstractTableModel that precludes editing; 作为参考, EnvTableTest是一个使用AbstractTableModel来阻止编辑的例子; the default implementation of isCellEditable() always returns false . isCellEditable()的默认实现始终返回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);
    }
}

There's a related example here . 有一个相关的例子在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM