2

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!

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
mctjl_1997
  • 163
  • 1
  • 3
  • 17

1 Answers1

4

For small amounts of data, consider java.util.prefs.Preferences.

Would you be able to provide me with some examples on how to use it?

Several examples are examined in the Preferences API Overview and the example cited here (API and code). Alternatively, consider javax.jnlp.PersistenceService, cited here, "for applications that are running in the restricted execution environment."

This minimal example updates a single cell by adding the previously saved value to the table and overriding the table model's setValueAt() implementation to save any change. Edit the table, quit and restart to see the effect.

image

package org.name.table;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.prefs.Preferences;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 * @see https://stackoverflow.com/a/34616583/230513
 */
public class TablePreference {

    private void display() {
        JFrame f = new JFrame("TablePreference");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new JScrollPane(new JTable(new PrefModel()) {

            @Override
            public Dimension getPreferredScrollableViewportSize() {
                return new Dimension(128, 32);
            }
        }));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private static class PrefModel extends DefaultTableModel {

        private static final int DEFAULT = 42;
        private static final String VALUE_NAME = "value";
        private final Preferences p = Preferences.userNodeForPackage(TablePreference.class);

        public PrefModel() {
            addColumn("A");
            addRow(new Object[]{p.getInt(VALUE_NAME, DEFAULT)});
        }

        @Override
        public void setValueAt(Object aValue, int row, int col) {
            super.setValueAt(aValue, row, col);
            p.putInt(VALUE_NAME, (int) aValue);
        }

        @Override
        public Class<?> getColumnClass(int col) {
            return getValueAt(0, col).getClass();
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new TablePreference()::display);
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045