简体   繁体   English

JTable内容编辑

[英]JTable content editing

I'm trying to edit the selected column in the selected row with button click like this 我正在尝试通过单击按钮编辑所选行中的所选列

contactTable.getModel().setValueAt(
    contactTable.getValueAt(contactTable.getSelectedRow(), 
    contactTable.getSelectedColumn()), 
    contactTable.getSelectedRow(), 
    contactTable.getSelectedColumn()
);

but before saving button click if I don't click in another column edit doesn't work, so what is the reason? 但是在保存按钮之前,如果我不单击其他列,则单击编辑不起作用,这是什么原因? and how can I fix it? 以及如何解决?

The edited value is still in the cell editor, not in the TableModel. 编辑后的值仍在单元格编辑器中,而不在TableModel中。 The value is automatically saved if you move to another cell in the table. 如果您移至表中的另一个单元格,该值将自动保存。

However, if you click on a button you must manually save the data. 但是,如果单击按钮,则必须手动保存数据。 This can be done in one of two ways: 这可以通过以下两种方式之一来完成:

  1. In the ActionListener of the button you can manually invoke the stopCellEditing() method of the editor 在按钮的ActionListener中,您可以手动调用编辑器的stopCellEditing()方法
  2. Add a property to the table to save the edits when the table loses focus. 当表失去焦点时,向表中添加属性以保存编辑。

Check out Table Stop Editing for more information and examples of these two approaches. 请查看表格停止编辑,以获取更多信息和这两种方法的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM