gpt4 book ai didi

java - 刷新JTable firetableDataChanged未执行

转载 作者:行者123 更新时间:2023-12-01 18:18:12 25 4
gpt4 key购买 nike

在我从 Excel 导入数据结构中的电子表格名称后,我尝试使用 fireTableChanged() 方法更新我的 JTable,但该方法未执行。我通过测试确认数据已正确导入,并且 jtable 应该具有必要的信息。

我必须做什么才能正确更新 JTable?我找到了该主题的其他几个链接,但没有一个对我有用:

Refresh 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();
}
}

最佳答案

  1. ((AbstractTableModel)ContentTable.getModel()).fireTableDataChanged(); 在模型定义之外调用,它不应该是,必须是代码的一部分,class void that重写 AbstractTableModel 及其方法

  2. 除了此方法重置模型的所有自定义属性之外,还有 JTable 方法的重要部分(例如覆盖 XxxTableCellRenderer/Editor)

  3. read API in part methods for fireTableXxxXxx ,所有 JTable/AbstractTableModel 的生命周期都有通知程序,请确保为每个操作/事件使用正确的通知程序

关于java - 刷新JTable firetableDataChanged未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28367522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com