AbstractTableModel包含正确的数据,但不会在fireTablDataChanged上更新

时间:2014-09-09 03:53:49

标签: java swing jtable abstracttablemodel

我多次搜索并看到这个问题,但没有一个解决方案有效。

我有AbstractTableModel我已扩展并致电AccountTableModel。代码是这样的。

import InvAcntItem.Account;
import java.util.LinkedList;
import javax.swing.table.AbstractTableModel;

public class AccountTableModel extends AbstractTableModel 
{
    LinkedList<Account> dataList;
    private String[] columnNames = {"Username", "Password"};

    public AccountTableModel()
    {
        dataList = new LinkedList<>();
    }

    public void setNewAccounts(LinkedList<Account> inAccs)
    {
        System.out.println("Syncing local account");
        LinkedList<Account> newList = new LinkedList<>();
        for(int i = 0; i < inAccs.size(); i++)
            newList.add(Account.getDeepCopy(inAccs.get(i)));
        System.out.println("done");
        this.dataList = newList;
        System.out.println("set");
        this.fireTableDataChanged();
        System.out.println("set");
    }

    @Override
    public int getRowCount() 
    {
        return dataList.size();
    }

    @Override
    public int getColumnCount() 
    {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int col, int row) 
    {
        System.out.println("GetValueAt!");
        Object retObj = null;
        Account rowAcc = dataList.get(row);
        switch(col)
        {
            case 0:
            {
                retObj = rowAcc.user;
            }
            break;
            case 1:
            {
                retObj = rowAcc.pass;
            }
            break;
        }
        return retObj;
    }
}

执行所有println语句,但UI永远不会更新。我甚至创建了一个按钮,当点击它时调用表模型fireDataChanged函数。

它还调用getValueAt函数并返回好的数据。

还有什么能阻止表重绘吗?

1 个答案:

答案 0 :(得分:3)

除此之外,getValueAt()的实现使行和列互换。修复此问题并添加假Account数据似乎有效。

image

import java.awt.EventQueue;
import java.util.LinkedList;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

/**
 * @see http://stackoverflow.com/a/25736893/230513
 */
public class Test {

    private static class Account {

        public Account() {
        }
    }

    class AccountTableModel extends AbstractTableModel {

        LinkedList<Account> dataList = new LinkedList<>();
        private String[] columnNames = {"Username", "Password"};

        public void setNewAccounts(LinkedList<Account> inAccs) {
            dataList.clear();
            dataList.addAll(inAccs);
            this.fireTableDataChanged();
        }

        @Override
        public int getRowCount() {
            return dataList.size();
        }

        @Override
        public int getColumnCount() {
            return columnNames.length;
        }

        @Override
        public Object getValueAt(int row, int col) {
            Account account = dataList.get(row);
            if (col == 0) {
                return account.getClass();
            }
            if (col == 1) {
                return account.hashCode();
            }
            return null;
        }
    }

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        AccountTableModel model = new AccountTableModel();
        LinkedList<Account> list = new LinkedList<>();
        list.add(new Account());
        list.add(new Account());
        list.add(new Account());
        model.setNewAccounts(list);
        f.add(new JScrollPane(new JTable(model)));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            new Test().display();
        });
    }
}