问题
I've searched and seen this question multiple times, and none of the solutions work.
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;
}
}
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.
It also calls the getValueAt
function and returns good data.
Is there anything else that would keep a table from redrawing?
回答1:
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();
});
}
}
来源:https://stackoverflow.com/questions/25736443/abstracttablemodel-contains-proper-data-but-will-not-update-on-firetabldatachang