Tomas Aušvicas
Tomas Aušvicas

Reputation: 25

JTable - TableModelEvent only values that changed

The porblem is that even I change cell value to the same exact value it counts as a change. So for example if I double click a cell and don't change its value my program proceeds to write it to the server.

How can I make that only cells that have their values changed, for instance (x -> y) write to server. (x -> x) do nothing.

Code below:

        Misc_Table.getModel().addTableModelListener(new TableModelListener() {

          public void tableChanged(TableModelEvent e) {

             int row = e.getFirstRow();
             int col = e.getColumn();

              Doo.addActionListener(new ActionListener(){

                public void actionPerformed(ActionEvent e) {

                    try{
                    Object value = model.getValueAt(row, col);

                        int selected = (int) Functions_ComboBox.getSelectedIndex();

                        if(selected == 0){
                            String val = (String) value;
                            int int_Val = Integer.parseInt(val);
                            BackEnd.WriteMultiple_16Bit(unitID, row, int_Val , 1);                      
                        }
                    } catch (NumberFormatException e1) {
                        JOptionPane.showMessageDialog(MISC,"Wrong number format.");
                    } catch (Exception e2) {    
                    }
                }

              });   
          }
        }); 

Upvotes: 1

Views: 849

Answers (1)

camickr
camickr

Reputation: 324118

You can use the Table Cell Listener.

The TableCellListener replaces the TableModelListener and will only generate events when the data has actually changed, not when the editor is stopped. You just provide an Action to be invoked when the data changes.

Upvotes: 1

Related Questions