gpt4 book ai didi

java - DefaultTableModel getValueAt 方法生成看似随机的结果

转载 作者:行者123 更新时间:2023-11-29 07:19:03 25 4
gpt4 key购买 nike

我有一个带有使用 DefaultTableModel 的 JTable 的 GUI。

声明了这些实例变量:

boolean doRun = false;
Class clazz;
Object obyect;
DefaultTableModel model;
ArrayList<String> al = new ArrayList();

表格是这样填充的:

public StatusGUI(Object invokerObject) {
initComponents();
setLocationRelativeTo(null);
clazz = invokerObject.getClass();
obyect = invokerObject;
String line;
try {
Field[] fields = clazz.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
if (!fields[i].isAccessible()) {
fields[i].setAccessible(true);
}
if (("" + fields[i].getType()).equals("class java.lang.String")) {
line = "String";
} else {
line = "" + fields[i].getType();
}

//Note: The first string in the Object is the description, which is left empty

model.insertRow(0, new Object[]{"", fields[i].getName(), line, "" + fields[i]});
}
} catch (Exception ex) {
ex.printStackTrace();
}
setVisible(true);
}

这会生成(在本例中)包含变量信息的 5 行。

我想在按下按钮时接收并存储这些变量的信息,代码如下:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
for (int i = 0; i < model.getRowCount(); i++) {
String description = "" + model.getValueAt(i, 0);
System.out.println("Description " + i + ": " + description);
String name = "" + model.getValueAt(i, 3);
if (!description.equals("") && description != null) {
al.add(description + "::" + name);
}
}
if (al.isEmpty()) {
JOptionPane.showMessageDialog(this, "No descriptions were added to any of the variables."
+ "\nThis could also be because no variables were found - if so, please see 'Help'");
} else {
new Thread(new SendThread(al, obyect)).start();
this.dispose();
}
}

向所有五行添加描述时,上述代码生成以下输出:

Description 0: d1
Description 1: d2
Description 2: d3
Description 3: d4
Description 4:

在JTable的第一行添加描述时,以上代码生成:

Description 0: 
Description 1:
Description 2:
Description 3:
Description 4:

这表明它识别了所有五行,但由于某些原因在从行中读取时出现了困惑。

我已经盯着同一行代码看了一个小时了,老实说,我看不出哪里出了问题。

提前致谢,迈克。

最佳答案

我的 Crystal 球显示您的表格仍在编辑中(第一个示例中的第 5 行,第二个示例中的第 0 行)。在执行的操作中首先提交该编辑。

if (table.isEditing()) {
table.getCellEditor().stopCellEditing();
}

关于java - DefaultTableModel getValueAt 方法生成看似随机的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6896792/

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