1

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) {    
                    }
                }

              });   
          }
        }); 
  • Before you write to the back end, you have to compare the int_Val from the GUI with the current value. You have to keep all the current values in a 2 dimensional array so you can get the current value by row and column. – Gilbert Le Blanc Mar 11 '16 at 14:30

1 Answers1

1

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thanks it works. But how can I use this with button? Because I need to press button to write new values. Pressing button sends all generated events and not last ones. – Tomas Aušvicas Mar 11 '16 at 22:39
  • When you press a button the editor is still active. The table only stops editing when you tab or click on a new cell in the table. You need to stop editing of the cell in order for the value to be saved. Check out [Table Stop Editing](https://tips4java.wordpress.com/2008/12/12/table-stop-editing/) for a couple of solutions. – camickr Mar 11 '16 at 23:00