import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
public class DataDisplay extends JFrame{
JTable table;
public static JLabel jl1= new JLabel("Activity ID");
public static JTextField jt1= new JTextField("Enter Activity ID");
public static JLabel jl2= new JLabel("Activity name");
public static JTextField jt2 = new JTextField("Enter Activity name");
public static JLabel jl3= new JLabel("Start Date");
public static JTextField jt3= new JTextField("Enter Start Date");
public static JLabel jl4 = new JLabel("End Date");
public static JTextField jt4= new JTextField("Enter End Date");
public static JLabel jl5 = new JLabel("Alarm Before");
public static JTextField jt5= new JTextField("enter alarm");
public static JLabel jl= new JLabel("HOURS");
public static JButton save = new JButton("SAVE");
public static JButton close = new JButton("CLOSE");
public Container c= this.getContentPane();
private static int counter=0;
DataDisplay(){
this.setTitle("Assignment 3");
this.setLayout(null);
this.setBackground(Color.MAGENTA);
c.add(jl1);
c.add(jt1);
c.add(jl2);
c.add(jt2);
c.add(jl3);
c.add(jt3);
c.add(jl4);
c.add(jt4);
c.add(jl5);
c.add(jt5);
c.add(jl);
jl1.setBounds(10, 10, 100, 100);
jt1.setBounds(100, 45, 100, 30);
jl2.setBounds(10,60,100,100);
jt2.setBounds(100,100,120,30);
jl3.setBounds(10,110,100,100);
jt3.setBounds(100,145,120,30);
jl4.setBounds(10,160,100,100);
jt4.setBounds(100,200,120,30);
jl5.setBounds(10,210,100,100);
jt5.setBounds(100,250,120,30);
jl.setBounds(230,210,100,100);
c.add(save);
save.setBounds(10,300,100,30);
save.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
int ce=0;
// TODO Auto-generated method stub
String tableHeader[] ={"Activity ID", "Activity name", "Start Date", "End Date","Alarm before (Hours)"};
String tableData[][]= new String[ce=counter+1][10];
table = new JTable(tableData, tableHeader);
String line=null;
BufferedReader b=null;
try {
b = new BufferedReader(new FileReader("Activity.txt"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
while ((line=b.readLine())!=null){
String[] s=null;
int row=1;
for(int counterc=1; counterc<=5; counterc++ ){//*ARRAY OUT OF BOUND EXCEPTION*
s=line.split("|",4);
}
***for(int loop=0;loop<=5;loop++){
//here gives exception
tableData[row++][loop]=s[loop];***
}
row++;
counter++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tableData[counter][0]=jt1.getText();
tableData[counter][1]=jt2.getText();
tableData[counter][2]=jt3.getText();
tableData[counter][3]=jt4.getText();
tableData[counter][4]=jt5.getText();
JScrollPane scrollPane = new JScrollPane(table);
c.add(scrollPane, BorderLayout.CENTER);
scrollPane.setBounds(10, 400, 700, 600);
counter++;
try{
FileWriter out= new FileWriter("Activity.txt",true);
BufferedWriter br= new BufferedWriter(out);
PrintWriter p= new PrintWriter(br);
p.println(jt1.getText()+"|"+jt2.getText()+"|"+jt3.getText()+"|"+jt4.getText()+"|"+jt5.getText());
p.close();
}
catch(IOException e){
e.getStackTrace();
}
}
});
c.add(close);
close.setBounds(200,300,100,30);
this.setBounds(100,100,1000,1000);
this.setVisible(true);
c.setVisible(true);
}
public static void main(String args[]){
DataDisplay dd= new DataDisplay();
}
}
Above program displays the table and saves into a file on screen whenever Save button is clicked. When user executes the program again then the last data isn't lost but is displayed on the screen. Data in the file is seperated by "|" operator. So what im trying to do is that loop reads line until end of file and splits it on the basis of pipe operator. And then a for loop displays data on the table. But probelem is my program is giving ARRAY OUT OF BOUND EXCEPTION and I couldn't solve it . So please help!