无法编辑看似任意的 JTable 列
正如标题所述,我有一个 JTable,但无法编辑“任意”列。我有 4 列,只有第一列是可编辑的。第一列有文件和一个特殊编辑器,接下来的两列有字符串,最后一列有整数。我使用的是自定义模型,并且从 isCellEditable
方法返回 true。当然,我首先查阅了几个网站寻求帮助,但我找不到任何有帮助的东西。我使用java源代码重写了几个JTable
方法并插入打印语句。特别是,我发现 table.editCellAt(row, col) 总是返回 false,因为从单元格编辑器返回的编辑组件始终为 null。因此,我很自然地尝试使用 table.setDefaultEditor(String.class, new MyEditor()) 替换编辑器。奇怪的是,这不起作用。 String 列的所有编辑器仍然是 JTable 默认使用的 GenericEditor。然后,我尝试通过执行以下操作将编辑器添加到每列:
TableColumnModel model = table.getColumnModel();
for(int i = 1; i < model.getColumnCount(); i++){
model.getColumn(i).setCellEditor(new MyEditor());
}
请注意,i 从 1 开始,因为第一列已经有合适的编辑器。我现在没有想法,所以我向 Stack Overflow 的好心人寻求帮助。
编辑:我正在使用 DefaultTableModel
,我只是覆盖 isCellEditable
以确保它始终返回 true (即使 DefaultTableModel
应该这样做默认)。我这样做是为了减少无用的、浪费的调试响应的数量。此外,一列可编辑但其他列不可编辑这一事实似乎表明问题出在其他地方。
编辑:问题似乎在于列创建。一位教授建议更改 setAutoCreateColumnsFromModel
,它似乎已经解决了这个问题。
As the title states, I have a JTable
and I am unable to edit "arbitrary" columns. I have 4 columns and only the first column is editable. The first column has files and a special editor, the next two columns have Strings, and the last column has Integers. I'm using a custom model, and I am returning true from the isCellEditable
method. Of course, I consulted several websites for help first, but I was unable to find anything that helped. I used the java source code to override several JTable
methods and insert print statements. In particular, I found that table.editCellAt(row, col) always returns false because the editing component returned from the cell editor is always null. So, I naturally tried to replace the editor using table.setDefaultEditor(String.class, new MyEditor())
. Oddly, that DID NOT work. All editors for the String columns were still the GenericEditor that JTable uses by default. I then tried adding the Editors to each column by doing the following:
TableColumnModel model = table.getColumnModel();
for(int i = 1; i < model.getColumnCount(); i++){
model.getColumn(i).setCellEditor(new MyEditor());
}
Note that i starts at 1 because the first column already has an appropriate editor. I'm out of ideas at this point so I came to the good people at Stack Overflow for some help.
Edit:I am using a DefaultTableModel
, I simply overrode isCellEditable
to make sure it always returns true (even though DefaultTableModel
is supposed to do that be default). I did this to reduce the amount of unhelpful, wasteful debugging responses. Also, the fact that one column is editable but others aren't would seem to indicate the problem is elsewhere.
Edit: It would appear the issue rested with column creation. A professor suggested changing setAutoCreateColumnsFromModel
and it seems to have fixed the issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我正在使用 DefaultTableModel,我只是覆盖 isCellEditable 以确保它始终返回 true (即使 DefaultTableModel 应该默认这样做)。我这样做是为了减少无用的、浪费的调试响应的数量。此外,一列可编辑但其他列不可编辑这一事实似乎表明问题出在其他地方。
I am using a DefaultTableModel, I simply overrode isCellEditable to make sure it always returns true (even though DefaultTableModel is supposed to do that be default). I did this to reduce the amount of unhelpful, wasteful debugging responses. Also, the fact that one column is editable but others aren't would seem to indicate the problem is elsewhere.
问题似乎在于列的创建。一位教授建议更改
setAutoCreateColumnsFromModel
,它似乎已经解决了这个问题。It would appear the issue rested with column creation. A professor suggested changing
setAutoCreateColumnsFromModel
and it seems to have fixed the issue.只需 5 行“自定义代码”即可测试 JTable 的使用。其余代码是您将来可能创建的任何 SSCCE 的模板。
It only take 5 lines of "custom code" to test the usage of a JTable. The rest of the code is a template for any SSCCE you might create in the future.