简体   繁体   English

如何为JTable Cell更改实现事件处理程序

[英]How to implement event handler for JTable Cell change

I would like to know how to implement the event handler for a jTable cell change. 我想知道如何为jTable单元更改实现事件处理程序。 Where i have this jtable and i dynamically add data to this jtable 我有这个jtable的地方,我动态地向这个jtable添加数据

The code segment for adding data is : 用于添加数据的代码段为:

JSONArray objResponseArray = (JSONArray) response.get("response");
DefaultTableModel model = (DefaultTableModel) tblAccounts.getModel();
for (int i = 0; i < objResponseArray.size(); i++) {
    JSONObject objTempAccount = (JSONObject) objResponseArray.get(i);
    Object[] row = {
         objTempAccount.get("Account ID").toString(),
         objTempAccount.get("Account Type").toString(),
         objTempAccount.get("Account Number").toString(),
         objTempAccount.get("Sort Code").toString(),
         objTempAccount.get("Balance").toString(),
         objTempAccount.get("Card").toString()
   };
   model.addRow(row);
}

In this table i have made the cells to be edited. 在此表中,我已进行了要编辑的单元格。 From that being said, How can i trigger an event handler to trigger when a cell is changed. 话虽这么说,我如何触发事件处理程序以在更改单元格时触发。 And when it is triggered i would like to get the entire row and update the database. 当它被触发时,我想获取整行并更新数据库。

IMPORTANT: I am using Netbeans and the event handlers are selected from the events in the properties window in the jtable element therefor please help me to implement this. 重要信息:我正在使用Netbeans,并且事件处理程序是从jtable元素的属性窗口中的事件中选择的,因此请帮助我实现这一点。

You can add a TableModelListener to your TableModel . 您可以将TableModelListener添加到TableModel An event will be generated when data is changed. 更改数据时将生成一个事件。 However it also generates an event if you start editing a cell and table to another cell without changing any data. 但是,如果您在不更改任何数据的情况下开始将一个单元格和表格编辑到另一个单元格,则它还会生成一个事件。

So instead you may want to consider the Table Cell Listener which will only generate an event when the data in the cell is actually changed. 因此,您可能要考虑使用表单元格侦听器 ,它仅在单元格中的数据实际更改时才会生成事件。

In both cases the event will contain the row/column of the cell changed, so you can easily get the date from other columns in the row by using the getModel().getValueAt(...) method of the table. 在这两种情况下,事件都将包含已更改的单元格的行/列,因此您可以使用表的getModel().getValueAt(...)方法轻松地从行中的其他列获取日期。

I am using Netbeans ... 我正在使用Netbeans ...

That should be irrelevant. 那应该无关紧要。 You should learn how to use Swing, not the IDE. 您应该学习如何使用Swing,而不是IDE。 If you ever switch the IDE the code may not be portable, especially the code to build the frame. 如果您曾经切换过IDE,则代码可能无法移植,尤其是用于构建框架的代码。 That is you will need to manually modify the code in another IDE anyway. 那就是您仍然需要在另一个IDE中手动修改代码。

    CellEditorListener ChangeNotification = new CellEditorListener() { 
   public void editingCanceled(ChangeEvent e) {

       //Action to be performed
    }


    public void editingStopped(ChangeEvent e) {
        //Action to be performed
    }
};
use the above code inside the class before the constructor.

Inside the constructor give

tablename.getDefaultEditor(String.class).addCellEditorListener(ChangeNotification); tablename.getDefaultEditor(String.class).addCellEditorListener(ChangeNotification);

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

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