[英]Java swing Jtable not updating through fireXXX using AbstractTableModel
I have a JTable in my GUI, which I wish to update dynamically. 我的GUI中有一个JTable,希望对其进行动态更新。 Associated to the Jtable is of course a TableModel, where I've extended the AbstractTableModel and overridden appropiate methods. 与Jtable关联的当然是TableModel,我在其中扩展了AbstractTableModel并覆盖了适当的方法。
I have four methods for my JTable: 我的JTable有四种方法:
When I run either AddRow or CopySelectedRow the table is 'one update behind': 当我运行AddRow或CopySelectedRow时,表是“落后一个更新”:
If I press newRow once, nothing happens visually. 如果我按一次newRow,则在视觉上什么也不会发生。
If I press newRow twice, the first one is shown while the second isn't. 如果我按两次newRow,则第一个显示,而第二个则不显示。
However, using deleteSelected or deleteAll updates the table when I click (ie not behind). 但是,使用deleteSelected或deleteAll可以在我单击(即不落后)时更新表。
Extract of my TableModel class: 我的TableModel类的摘录:
public class TableModel extends AbstractTableModel {
private List<String[]> data;
public TableModel() {
data = new ArrayList<String[]>();
}
...
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
data.get(rowIndex)[columnIndex] = aValue.toString();
fireTableCellUpdated(rowIndex, columnIndex);
}
public void addRow(String[] aValue) {
data.add(aValue);
fireTableRowsInserted(data.size()-1, data.size()-1);
}
public void copyRow(int rowIndex) {
addRow(data.get(rowIndex));
}
public void removeRow(int rowIndex) {
data.remove(rowIndex);
fireTableRowsDeleted(rowIndex, rowIndex);
}
And how I call them: 以及我如何称呼他们:
JButton newRow = new JButton("New row");
newRow.addActionListener(new ActionListener() {
// Handling newRow event
public void actionPerformed(ActionEvent e) {
tableModel.addRow(new String[]{"", "", "", "", "", "", "", "", "", "", ""});
}
});
JButton copyRow = new JButton("Copy selected row");
copyRow.addActionListener(new ActionListener() {
// Handling copyRow event
public void actionPerformed(ActionEvent e) {
if (table.getSelectedRow() != -1) {
tableModel.copyRow(table.getSelectedRow());
}
}
});
JButton deleteRow = new JButton("Delete selected row");
deleteRow.addActionListener(new ActionListener() {
// Handling deleteRow event
public void actionPerformed(ActionEvent e) {
if (table.getSelectedRow() != -1) {
tableModel.removeRow(table.getSelectedRow());
}
}
});
JButton deleteAllRows = new JButton("Delete all rows");
deleteAllRows.addActionListener(new ActionListener() {
// Handling deleteAllRows event
public void actionPerformed(ActionEvent e) {
for (int i = tableModel.getRowCount() - 1; i >= 0; i--) {
tableModel.removeRow(i);
}
}
});
EDIT: 编辑:
I chose to use an AbstractTableModel because I had the same problem with the DefaultTableModel (whenever I added a row, it wasn't added until the next was 'added'), and with an AbstractTableModel I would be able to fire change events myself. 我之所以选择使用AbstractTableModel,是因为DefaultTableModel存在相同的问题(每当我添加一行时,直到下一行被“添加”时才添加它),并且使用AbstractTableModel,我可以自己触发更改事件。 However, it didn't fix a thing. 但是,它没有解决任何问题。 Can anyone please shed some light on my issue here? 有人可以在这里阐明我的问题吗? I'd be happy to elaborate more on the case, should anyone need some more information. 如果有人需要更多信息,我很乐意对此事进行详细说明。
A method 一个方法
FireTableDataChanged();
detects any kind of alteration in table data object and update the GUI respectively, you can try that instead of 检测表数据对象中的任何类型的更改并分别更新GUI,您可以尝试使用
fireTableRowsInserted();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.