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