Dear all,

I was told to write an application for my assignment that would keep track of contracts. The problem is, I have no clue how I can make this application write JTable into txt file every 5 seconds and wheever application would open, it would read the txt file and display the data in JTable in the correct order.

Here is my code that I am working on currently. Please note that I am still a beginner and this is my first application, so sorry about the untidiness.

import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.TimerTask;
import javax.swing.*;
import javax.swing.table.TableModel;
public class Main extends JFrame
{
    // Instance attributes used in this application
    private JPanel topPanel;
    private JTable table;
    private JScrollPane scrollPane;
    // Constructor of main frame
    public Main() throws Exception
    {
        // Set the frame characteristics
        setTitle( "Contract TRACKER" );
        setSize( 900, 800 );
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setBackground( Color.gray );
        //Adding save button
        JButton save = new JButton("Save");
        // Create a panel to hold all other components
        topPanel = new JPanel();
        topPanel.setLayout( new BorderLayout() );
        getContentPane().add( topPanel );
        // Create columns names
        String columnNames[] = { "Contract", "Description", "Deadline", "Contact(s)" };
        // Create some  test data
        String dataValues[][] =
        {
        //    { "12", "234", "67", "122" },
         //   { "-123", "43", "853", "122" },
        //   { "93", "89.2", "109", "fdf" }
        };
        // Create a new table instance
        table = new JTable( dataValues, columnNames );
        //Adding save button
        // Add the table to a scrolling pane
        scrollPane = new JScrollPane( table );
        topPanel.add( scrollPane, BorderLayout.CENTER );

        BufferedWriter bfw = new BufferedWriter(new FileWriter("Core_DATA.txt"));
        for(int i = 0 ; i < table.getColumnCount() ; i++)
        {
            bfw.write(table.getColumnName(i));
            bfw.write("t");
        }
        for (int i = 0 ; i < table.getRowCount(); i++)
        {
            bfw.newLine();
            for(int j = 0 ; j < table.getColumnCount();j++)
            {
                bfw.write((String)(table.getValueAt(i,j)));
                bfw.write("t");;
            }
        }
        bfw.close();
    }
        // Main entry point for this example
        public static void main(String args[] ) throws Exception
        {
            // Create an instance of the test application
            Main mainFrame = new Main();
            mainFrame.setVisible( true );
        }
    } 

I would really appreciate it if you could help.

Many thanks,
Shayan

I would really appreciate it if someone could please help me or at least give me some links or a brief idea on what to do.

You have written the array of data to a text file, so loading it is a question of
Reading each line from the text file (see any tutorial for reading lines from a text file)
Splitting that line into fields using the tabs as separators (see String's split method)
Storing the fields into your data array

Doing something every 5 seconds (if that is really waht you want) can be done with a java.util.Timer (see the API doc) which will call your save method every 5,000 miliiSecs

commented: Brilliant, thanks! I will try it tomorrow (It's 10 PM local) and I will keep you updated. +0
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.