简体   繁体   English

在JTable中编辑单元格值时更新mysql

[英]Update mysql when cell value is edited in JTable

Well I have a JTable in my app that fetches records from a MySql database adn displays them in a sorted order. 好吧,我的应用程序中有一个JTable,可从MySql数据库中获取记录,并按排序顺序显示它们。 Till now I have been using a separate JTextArea and a JComboBox to allow the users to edit the table, something like 到目前为止,我一直在使用单独的JTextArea和JComboBox来允许用户编辑表,例如 在此处输入图片说明

so whenever someone clicks on a row in the table the ID of the record is automatically updated below in a JLabel that lies before the JComboBox. 因此,只要有人单击表中的行,记录的ID就会在JComboBox之前的JLabel中自动更新。

The question is how can I allow users to simply double click and edit values in the cells which would automatically fire a SQL Update query and update the same in the database. 问题是我如何允许用户简单地双击并编辑单元格中的值,这将自动触发SQL Update查询并更新数据库中的查询。 I want to allow this on certain Columns only not every Column. 我只允许某些列而不是每个列都允许这样做。

A response with a code example would be highly appreciated. 带有代码示例的响应将受到高度赞赏。

how can I allow users to simply double click and edit values in the cells which would automatically fire a SQL Update query and update the same in the database. 如何允许用户简单地双击并编辑单元格中的值,这将自动触发SQL Update查询并更新数据库中的查询。

You can use a TableModelListener to be notified of changes to the TableModel . 您可以使用TableModelListener通知更改的TableModel

I want to allow this on certain Columns only not every Column. 我只允许某些列而不是每个列都允许这样做。

The row/column is found in the TableModelEvent so you check the column that changed before invoking the SQL update. TableModelEvent找到该行/列,因此您在调用SQL更新之前检查已更改的列。

One potential problem with this is that a TableModelEvent is generated even if the data isn't changed. 一个潜在的问题是,即使不更改数据,也会生成TableModelEvent。 That is if you place the cell in edit mode and, for instance, tab to the next cell without change the data and event will still be generated. 就是说,如果您将单元格置于编辑模式,例如,在不更改的情况下跳至下一个单元格,则仍将生成数据和事件。

To get around this problem you may want to consider using the Table Cell Listener . 要解决此问题,您可能需要考虑使用Table Cell Listener When using this class the event is only generated if the data has actually been changed. 使用此类时,仅在实际更改数据时才生成事件。

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

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