How do you load array values into a JTable
such that whenever the form is opened, it shows the previous values of the table? I do not want to connect the form to any databases.
This is my code so far, it allows me to enter texts to the text field, and when I click "create customer" button, it stores the value into the JTable
. However if I exit and reopen the form, the data previously in the table disappears. And i have done some research, but it seems like connecting netbeans to a database is the only way to save and retrieve data. However, I believe that storing data into the array is possible too, but Ii do not know how to bring out the value in the array into the table.
I need some help. It is for my school project.
FYI, some of the notations I've used: rbtn = radiobutton, tb = textfields, lbl = label
public class Customer extends javax.swing.JFrame {
String gender;
int year = Calendar.getInstance().get(Calendar.YEAR);
int month = Calendar.getInstance().get(Calendar.MONTH);
int day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
int m=10; //array memory size
String[] name = new String[m];
String[] age = new String[m];
String[] genderm = new String [m];
String[] id = new String [m];
String[] mobile = new String [m];
String[] email = new String [m];
String[] address = new String [m];
String[] date = new String [m];
String[] photo = new String[m];
public Customer() {
initComponents();
tbdate.setText(day+"/"+(month+1)+"/"+year);
lblphoto.setIcon(null);
}
private void btncreateActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (rbtnmale.isSelected()){
gender = "Male";
}
else if (rbtnfemale.isSelected()){
gender = "Female";
}
DefaultTableModel model = (DefaultTableModel) jtablecustinfo.getModel();
model.addRow(new Object[]{tbname.getText(),tbage.getText(),gender,tbid.getText(),tbmobile.getText(),tbemail.getText(),tbaddress.getText(),tbdate.getText(),lblphoto.getIcon()});
for(int i=0;i<m;i++){
name[i]=tbname.getText();
age[i] = tbage.getText();
genderm[i]=gender;
id[i]=tbid.getText();
mobile[i]=tbmobile.getText();
email[i]=tbemail.getText();
address[i]=tbaddress.getText();
date[i]=tbdate.getText();
photo[i]= tbimage.getText();;
}
//Reset everything after creation
JOptionPane.showMessageDialog(null,"Successfully Created Customer");
tbname.setText("");
tbage.setText("");
tbid.setText("");
tbmobile.setText("");
tbemail.setText("");
tbaddress.setText("");
tbdate.setText("");
rbtnmale.setSelected(false);
rbtnfemale.setSelected(false);
tbdate.setText(day+"/"+(month+1)+"/"+year);
gender = "";
tbimage.setText("");
lblphoto.setText(" -Import photo-");
lblphoto.setIcon(null);
}
I have included the main points of my code, hope it is sufficient to view!