hey guys,

I'm making a small app that make a Jable and saves the table model then loads the table model and set it to the table interface and both saving and loading is in seperate frames.

// Saving the info 
    String ufname = Fname.getText();
    String ulname = Lname.getText();
    String ubalance = balance.getText();
    String cdate = datetxt.getText();


    if (ufname.equals("") || ulname.equals("") || ubalance.equals("")) {
        final JPanel panel = new JPanel();
        JOptionPane.showMessageDialog(panel, "Entries Empty! Fill in the required entries.", "Error",
                JOptionPane.ERROR_MESSAGE);
    } else {

        //making a saving table
        String[] header = {"First Name", "Last Name", "Balance", "Data"};
        String[][] elements = {
            {ufname, ulname, ubalance, cdate}
        };

        String filename = "table.file";
        JTable table = new JTable(elements, header);
        TableModel tm = table.getModel();
            try {
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename));
                oos.writeObject(tm);
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
                return;
            }
        final JPanel panel2 = new JPanel();
        JOptionPane.showMessageDialog(panel2, "Data Has Been Saved!", "SAVED",
                JOptionPane.HEIGHT);

        setVisible(false);
    }

//NOT this code is in NetBeans GUI Builder so it looks a little bit funky

and about my code in loading is

String filename = "table.file";
	TableModel tm;
		try {
			ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename));
			tm = (TableModel) ois.readObject();
		}
		catch(Exception e) {
			e.printStackTrace();
			return;
		}
		tabledis.setModel(tm);

Hope I get some help here.

Recommended Answers

All 4 Replies

What exactly is your question?

I want to add new information when ever the user add one and when that information entered the table update it self not wait until the program restated and also not to delete the old information and write the new one on the top of it.

Read all the info, and check if it is the same info in your table. Then, add the rest of the table to the file.
You can also empty the file completely, and write your entire table.

and how to do that?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.