且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AbstractTableModel包含适当的数据,但不会在fireTablDataChanged上更新

更新时间:2023-12-03 12:10:10

在其他方面,您对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://***.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();
        });
    }
}