Reputation: 11
Below is the code for my jTable created by the GUI builder included in netbeans. I set one of the columns to editable (due date column for a library app) Yes, the user can edit that column and type stuff in, but once the program is closed, the table doesn't save what the user imputed into those columns! I've tried a lot of stuff but nothing seems to work! any help would be appreciated!
adultFictionTable = new javax.swing.JTable();
adultFictionTable.setAutoCreateRowSorter(true);
adultFictionTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
adultFictionTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{"Book 1", "8787987897987", "3/1/11"},
{"The Rows And", "2131223", "2/1/11"}
},
new String [] {
"Book Name", "ISBN", "Due Date"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.String.class, java.lang.String.class
};
boolean[] canEdit = new boolean [] {
false, false, true
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
adultFictionTable.setName("adultFictionTable"); // NOI18N
adultFictionTable.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
adultFictionTableKeyPressed(evt);
}
});
jScrollPane1.setViewportView(adultFictionTable);
adultFictionTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("adultFictionTable.columnModel.title0")); // NOI18N
adultFictionTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("adultFictionTable.columnModel.title1")); // NOI18N
adultFictionTable.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("adultFictionTable.columnModel.title2")); // NOI18N
Upvotes: 1
Views: 2033
Reputation: 59634
The answer is simple. Each time your application is started, your code fills the table with the same DefaultTableModel
object which contains the default values you have set up in NetBeans.
If you want the application to remember the entered values, you need to retrieve them from the model and safe them in a file (for example). Then, when the application is started again, you should read those values from the file and create a NEW DefaultTableModel
and put the values in it. Then you should explicitely set this model in your table. It will override the existing one.
EDIT
Here is an example showing how to retrieve the model of a 3 column * 16 lines table in order to clear its content by setting empty values in it:
// Retrieving the model
TableModel model = jTableTranslation.getModel();
// Clearing the model
for (int i=0;i<16;i++) {
for (int j=0;j<3;j++) {
model.setValueAt("", i, j);
}
}
Table models also have a .getValueAt(...)
method to retrieve content.
Upvotes: 3