简体   繁体   English

Java Swing:在JTable中编辑单元格时如何获取上一个值和当前值?

[英]Java Swing: How can I get the previous value and the current value when editing a cell in JTable?

I have a table that contains data from mysql.我有一个表,其中包含来自 mysql 的数据。 I want to modify this data when I select any row and then the modification is done to the database.我想在我 select 任何行时修改此数据,然后对数据库进行修改。

链接中的图片

You could useTableCellListener to identify when an actual change in the cell data occurs, allowing you to retrieve the current value of the cell (the new value) using tcl.getNewValue() , as well as the old one using tcl.getOldValue() .您可以使用TableCellListener来识别单元格数据何时发生实际更改,从而允许您使用tcl.getNewValue()检索单元格的当前值(新值),以及使用tcl.getOldValue()旧值. Additionally, it allows you to get the row and column indices of the cell that has been modified.此外,它还允许您获取已修改单元格的rowcolumn索引。

Example as given in the source above:如上面来源中给出的示例:

Action action = new AbstractAction()
{
    public void actionPerformed(ActionEvent e)
    {
        TableCellListener tcl = (TableCellListener)e.getSource();
        System.out.println("Row   : " + tcl.getRow());
        System.out.println("Column: " + tcl.getColumn());
        System.out.println("Old   : " + tcl.getOldValue());
        System.out.println("New   : " + tcl.getNewValue());
    }
};

TableCellListener tcl = new TableCellListener(table, action);  // where table is the instance of your JTable

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

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