【发布时间】:2015-04-06 16:46:10
【问题描述】:
在我从数据结构中的 excel 导入电子表格的名称后,我尝试使用 fireTableChanged() 方法更新我的 JTable,但该方法未执行。我通过测试确认数据已正确导入,并且 jtable 应该具有必要的信息。
我需要做什么才能正确更新 JTable? 我找到了该主题的其他几个链接,但没有一个对我有用:
How to make JTable show refreshed data after updating database?
JTable How to refresh table model after insert delete or update the data.
AbstractDataTable fireTableDataChanged() does not refresh jtable
Can't refresh my JTable with new data
型号:
public class Model extends Observable {
String[][] data;
List<Arbeitsmappe> AMList = new LinkedList<>();
.....
public void setAMList(List<Arbeitsmappe> aMList) {
AMList = aMList; //new List replace the old
this.getData(); //The 2dimensional Array is filled with the names from the list
setChanged();
notifyObservers(Controller.Command_Excel_Eingelesen);
}
}
查看:
JTextField cellEditorTF = new JTextField();
cellEditorTF.setEditable(false);
DefaultCellEditor cellEditor = new DefaultCellEditor(cellEditorTF);
ContentTable = new JTable(model.getData(), Model.columnNames);
//Cell Editable FALSE
ContentTable.getColumnModel().getColumn(0).setCellEditor(cellEditor);
//Single Interval Selection
ContentTable.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
//Cell Listener - When Cell is edited the new informationen is safed in AMLISt
Action action = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
TableCellListener tcl = (TableCellListener)e.getSource();
Model.AMList.get(tcl.getRow()).Speichername = String.valueOf(tcl.getNewValue());
// System.out.println("Row: " + tcl.getRow() + " " + Model.data[tcl.getRow()][1]);
}
};
TableCellListener tcl = new TableCellListener(ContentTable, action);
JScrollPane scrollPane = new JScrollPane(ContentTable);
ContentTable.setFillsViewportHeight(true);
ContentTable.getTableHeader().setReorderingAllowed(false);
this.add(BorderLayout.NORTH,ButtonPanel);
this.add(BorderLayout.SOUTH,scrollPane);
}
@Override
public void update(Observable arg0, Object arg1) {
if(arg0 instanceof Model){
Model model = (Model) arg0;
String cmd = (String) arg1;
if(cmd.equals(Controller.Command_Excel_Eingelesen)){
((AbstractTableModel)ContentTable.getModel()).fireTableDataChanged();
ContentTable.repaint();
this.repaint();
}
}
【问题讨论】:
-
如果您没有尽快获得帮助,请考虑创建并发布Minimal, Complete, and Verifiable Example Program,您可以在其中将代码压缩为仍可编译和运行的最小位,没有外部依赖项(例如需要链接到数据库或图像),没有与您的问题无关的额外代码,但仍能演示您的问题。
-
还要注意,您不应该在您的表模型上调用任何
fireXxxx()方法。这些只能在您的表模型本身内部调用。你的设计似乎被你这样做破坏了。
标签: java swing jtable abstracttablemodel