简体   繁体   English

TableModelListener有时只能工作

[英]TableModelListener only works sometimes

I have a database application that uses a jTable (using netbeans), I have added a table model listener to the jTable so when I edit the jTable it also edits the database (MySQL). 我有一个使用jTable的数据库应用程序(使用netbeans),我向jTable添加了一个表模型侦听器,因此当我编辑jTable时,它还会编辑数据库(MySQL)。 It works however when the user clicks off the jTable and clicks a button it stops working. 但是,当用户单击jTable并单击一个按钮时,它将停止工作。

Edit: Ive realised that it only stops working when I run "jTable1.setModel(DbUtils.resultSetToTableModel(mySql.UpdateTable()));" 编辑:我已经意识到,只有当我运行“ jTable1.setModel(DbUtils.resultSetToTableModel(mySql.UpdateTable()))“时,它才会停止工作。 to update the table. 更新表。 When I press a button that runs this line it stops working, any other button or any other time and it works. 当我按下运行此行的按钮时,它将停止工作,在其他任何时间或任何其他时间都可以工作。

to update table: 更新表:

    public ResultSet UpdateTable(){

        Connection con = connect();
    try{
        Statement s = con.createStatement();
        ResultSet resultset;
        resultset =  s.executeQuery("select * from customera");
        return resultset;
        }
    catch(SQLException e){
        System.out.println(e.getMessage());
    }
    return null;
}

} }

tablemodellistener class: tablemodellistener类:

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

public class TableActionListener implements TableModelListener{

Rob1 r;

public TableActionListener(){
    r = new Rob1();
    }

public void tableChanged(TableModelEvent e){
    System.out.println("action");
    int id = r.getID();
    int column = e.getColumn();
    int row = e.getFirstRow();



    }

}

added table model listener like so in constructor: 在构造函数中添加表模型侦听器,如下所示:

jTable1.getModel().addTableModelListener( new TableActionListener());

to get the row clicked i've added this code: 为了获得单击的行,我添加了以下代码:

int id;

private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {                                     
    // TODO add your handling code here:
    System.out.println(evt.getClickCount());

        int row = jTable1.getSelectedRow();
        int col = jTable1.getSelectedColumn();
        id = (int)jTable1.getModel().getValueAt(row, 0);

} 

to return current id: 返回当前ID:

 public int getID(){
    return id;
}

It works however when the user clicks off the jTable and clicks a button it stops working. 但是,当用户单击jTable并单击一个按钮时,它将停止工作。

By default the table cell editor is only stopped editing when you move to another cell in the table. 默认情况下,表格单元格编辑器仅在您移至表格中的另一个单元格时才停止编辑。

So when you click on another component you need to tell the table to stop editing. 因此,当您单击另一个组件时,您需要告诉表停止编辑。 Then the data will be saved and the TableModelListener will be invoked. 然后,将保存数据并调用TableModelListener。

Check out Table Stop Editing for two ways to do this: 查看“ 表停止编辑”中提供两种方法来执行此操作:

1) Add a property to the table: 1)在表格中添加一个属性:

JTable table = new JTable(...);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

2) Add code to the listener of your button: 2)将代码添加到按钮的侦听器:

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

You may also want to check out Table Cell Listener which may be more appropriate to use instead of the TableModelListener. 您可能还想签出Table Cell Listener ,它可能更适合代替TableModelListener使用。

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

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