gpt4 book ai didi

Java JTable/DefaultTableModel java.lang.ArrayIndexOutOfBoundsException

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

在我的应用程序中,我正在使用包含 DefaultTableModel 的 JTable。一切都工作正常,直到我想清除我的表并从新的 DefaultTableModel 开始。清算过程是这样完成的:

public static JTable table = new JTable;
public static CustomTableModel model = new CustomTableModel();

[...]

table.setModel(new DefaultTableModel());
model = new CustomTableModel();
model.createEmptyModel();
table.setModel(model);
// model.save(config.getDatabaseFile(), table); // <<< Reference A
model.initColumModel(table);

到目前为止一切顺利。现在,我的 CustomTableModelDefaulTableModel 扩展并添加了一些自定义方法。其中之一是 createEmptyModel() ,如下所示:

public void createEmptyModel() {
model = new DefaultTableModel(new Object[][] {},
new String[] { "Lfd. Nr.", "FB Nr.", "Auftr. / Meld. Nr.", "Betra Nr.", "Datum", "Bahnhof", "Str. Km.",
"Sprz.", "Arb. Zeit", "Mitarbeiter", "Auftrag / Objekt(e)", "Anmerkungen", "Fertig" }) {

private static final long serialVersionUID = -4325766838779239822L;
@SuppressWarnings("rawtypes")
Class[] columnTypes = new Class[] { Integer.class, String.class, Long.class, Long.class, String.class,
String.class, String.class, String.class, String.class, String.class, String.class, String.class, Boolean.class };

@SuppressWarnings({ "unchecked", "rawtypes" })
public Class getColumnClass(int columnIndex) {
return columnTypes[columnIndex];
}
};
// this sequentialNumber is just some object which contains a counter. Not important here!
sequentialNumber.reset();
}

通过这种方法,我使用所有需要的 vector 和类型初始化模型。

当调用 model.initColumModel(table) get 时,我的应用程序因以下异常而崩溃:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.table.DefaultTableColumnModel.getColumn(Unknown Source)
at somepacket.CustomTableModel.initColumModel(CustomTableModel.java:36)
at somepacket.DBDatabaseSystem.createNewDatabase(DBDatabaseSystem.java:416)
at somepacket.DBDatabaseSystem$4.actionPerformed(DBDatabaseSystem.java:155)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

我的initColumModel方法声明如下:

public void initColumModel(JTable table) {
// doing this here to avoid calling table.getColumnModel multiple times
TableColumnModel columnModel = table.getColumnModel();

columnModel.getColumn(0).setResizable(false);
columnModel.getColumn(0).setPreferredWidth(50);
columnModel.getColumn(1).setResizable(false);
columnModel.getColumn(1).setPreferredWidth(50);
columnModel.getColumn(2).setResizable(false);
columnModel.getColumn(2).setPreferredWidth(81);
columnModel.getColumn(3).setResizable(false);
columnModel.getColumn(3).setPreferredWidth(81);
columnModel.getColumn(4).setPreferredWidth(81);
columnModel.getColumn(5).setPreferredWidth(134);
columnModel.getColumn(6).setPreferredWidth(123);
columnModel.getColumn(7).setPreferredWidth(95);
columnModel.getColumn(8).setPreferredWidth(95);
columnModel.getColumn(9).setPreferredWidth(226);
columnModel.getColumn(10).setResizable(true);
columnModel.getColumn(10).setPreferredWidth(300);
columnModel.getColumn(11).setResizable(true);
columnModel.getColumn(11).setPreferredWidth(102);
columnModel.getColumn(12).setResizable(false);
columnModel.getColumn(12).setMinWidth(45);
columnModel.getColumn(12).setMaxWidth(45);
}

据我所知,问题是我的 CustomTableModel 创建后没有列。所以我尝试的是向表中添加一个新行,然后调用 initColumModel ,但这没有帮助。应用程序仍然崩溃。防止崩溃的唯一方法是在调用 initColumModel 方法之前保存模型。但这没有意义,你自己看看,这是我的保存方法,它不会改变模型上的任何内容,也不会添加任何列:

[...]

try {
// undo all selections to prevent user from editing while saving
table.clearSelection();
// prevent saving when user is editing a cell
if (table.isEditing()) {
table.getCellEditor().stopCellEditing();
}

fos = new FileOutputStream(filename);
out = new ObjectOutputStream(fos);

table.setModel(new DefaultTableModel());
// detach model from table prior serializing
out.writeObject(model);
// re-attach table model
table.setModel(model);
// set layout again
initColumModel(table);

out.writeObject(sequentialNumber);

[...]

保存模型后,它会出现在 JTable 中,并且一切正常。怎么会这样?在调用 initColumModel 之前,我尝试手动从表中分离并重新附加模型,但这也没有帮助。

基本上我现在迷失了,我该如何解决我的问题?还是我做了一些明显错误的事情?

提前致谢!

最佳答案

如果您只想清除模型中的数据,那么您只需使用:

model.setRowCount(0);

如果您想用新的结构(即新的列标题和数据)替换 TableModel,那么您只需创建一个新的 TableModel:

TableModel model = new DefaultTableModel(...);
table.setModel( model );

From what I can the problem is that my CustomTableModel has no columns after it's creation

如果正确实现 getColumnCount() 和 getColumnNames() 方法,它肯定有列。

在您实际为表设置模型之前,不会创建 JTable 的 TableColumnModel。当您使用 new DefaultTableModel() 时,您将创建一个具有零列的 TableColumnModel。因此,如果您希望存在列,请向表中添加适当的 TableModel。

// detach model from table prior serializing

为什么要把模型从 table 上拆下来?这是不必要的。

您可能希望使用 XMLEncoder 来代替序列化 DefaultTableModel,这是长期存储对象的推荐方法。请参阅:Saving content of Interactive JTable to .txt file to read it upon next run有关此方法的工作示例。

关于Java JTable/DefaultTableModel java.lang.ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31561550/

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