AbstractTableModel包含适当的数据,但不会在fireTablDataChanged上更新 [英] AbstractTableModel contains proper data but will not update on fireTablDataChanged

查看:389
本文介绍了AbstractTableModel包含适当的数据,但不会在fireTablDataChanged上更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已多次搜索并看到此问题,但没有一种解决方案有效.

I've searched and seen this question multiple times, and none of the solutions work.

我有一个已经扩展并称为AccountTableModelAbstractTableModel.代码就是这样.

I have an AbstractTableModel that I've extended and called AccountTableModel. The code is as such.

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函数.

All of the println statements are executed, yet the UI never updates. I have even gone so far as to create a button that when clicked calls the table models fireDataChanged function.

它还调用getValueAt函数并返回正确的数据.

It also calls the getValueAt function and returns good data.

还有其他可以阻止表格重绘的东西吗?

推荐答案

在其他方面,您对getValueAt()的实现将行和列互换.解决该问题并添加伪造的Account数据似乎可以正常工作.

Among other things, your implementation of getValueAt() has the row and column interchanged. Fixing that and adding fake Account data seems to work.

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();
        });
    }
}

这篇关于AbstractTableModel包含适当的数据,但不会在fireTablDataChanged上更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆