问题描述
我已多次搜索并看到此问题,但没有一种解决方案有效.
I've searched and seen this question multiple times, and none of the solutions work.
我有一个已经扩展并称为AccountTableModel
的AbstractTableModel
.代码就是这样.
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上更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!