0

I have a JTable whose cells are editable. However if i edit a cell and refresh the table. The changes are not saved. This is how i have defined the table:

    String [] columnNames = {"Application number",
                            "Name",
                            "date",
                            "fileLoc",
                            "Country"};
    //Data for the table
    String [][] data = table.tableArray;//tableArray is an array of array of strings.

    TableModel model = new DefaultTableModel(data, columnNames);
    JTable mainTable = new JTable(model);
    model.isCellEditable(data.length,columnNames.length);
    contentPane.add(new JScrollPane (mainTable));

I've had a look online but can't seem to find any advice on saving the changes made to a cell. Any help would be very welcome!

Sam
  • 7,252
  • 16
  • 46
  • 65
Hoggie1790
  • 319
  • 2
  • 7
  • 19

1 Answers1

1

I guess i'm not refreshing the table as such. I use frame.dispose() and then create a new frame with the table in.

  • then you lost all changes made in the current DefaultTableModel

  • don't top create a new JFrame with a new DefaultTableModel and a new JTable

  • all changes from TableCellEditor are changes dispayed in JTables view

  • JTable (with its model) is prepared for this job, don't to reacreate these Objects on runtime

  • DefaultTableModel has implemented all notifiers, there no needed to override any others events, nor to fireXxxXxx() programatically, but those events are required for AbstractTableModel

  • define add this code line for your JTablemainTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

  • DefaultTableModel respesenting your required 2D array

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Thanks for the advice. Is it possible to make changes made in the JTable save to the 2D arraylist the table is based on? The reason being i will need to save this array and reload it when the program is restarted. Thanks. – Hoggie1790 Mar 14 '13 at 16:48
  • @Hoggie1790 Look at [getDataVector](http://docs.oracle.com/javase/7/docs/api/javax/swing/table/DefaultTableModel.html#getDataVector%28%29) to get the data that model is using. – Amarnath Mar 14 '13 at 16:59
  • +1, the key to solving the problem is the table client property as described in more detail in [Table Stop Editing](http://tips4java.wordpress.com/2008/12/12/table-stop-editing/). – camickr Mar 14 '13 at 18:13