Home > Blockchain >  java swing : How can I get the previous value and the current value when editing a cell in JTable?
java swing : How can I get the previous value and the current value when editing a cell in JTable?

Time:02-13

I have a table that contains data from mysql. I want to modify this data when I select any row and then the modification is done to the database

Table image in the link

CodePudding user response:

You could use TableCellListener 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(). Additionally, it allows you to get the row and column indices of the cell that has been modified.

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
  • Related