Populating JTableModel with Multiple Text Files using 2D Array
Dear Members,
I would be EXTREMELY grateful if someone who knows what they are doing could look at this and tell me what im doing wrong.
The scenario is this:
I have a folder /save/ which contains a lots of text files, all named in the same format, an example of the text file called "Jon Doe.txt" it contains:
First Name:Jon
Surname:Doe
Sex:Female
Ability:High
Topic:
Exercise:
What I want to be able to do... is populate my JTable (through the JTabelModel) with a row for EVERY file, and colums for each value (eg in the case above, Jon, Doe, Female, High, <null>, <null>)
I have almost mastered this but I am having trouble with the 2D Array[][] I am using. I seem to be able to output the array to the screen but when i call the array, for example calling array[2][2] (which should look up file name 2, field 2) Instead of getting this i get "null".
Anyone got any ideas? The code for the TableModel is below:
import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.StringTokenizer;
import javax.swing.table.AbstractTableModel;
import com.sun.java.util.*;
publicclass MyTableModelextends AbstractTableModel
{
//Initialise Array
String[][] array;
//Create File Object for Save Folder
File dir =new File("save");
// Create Variables for File Reader
int counter=0;{
// Check if file is a directory
if(dir.isDirectory())
{
// Create String array of all files in drectory
String s[] = dir.list();
// For every file in the directory...
int numberoffiles = s.length;
array =new String[numberoffiles][4];
for (int i=0; i<numberoffiles; i++)
{
// Read in each file
try
{
String directory = ("save/"+s[i]);
BufferedReader in =new BufferedReader(new FileReader(directory));
String str;
for (int j=0; j><5; j++)
{
while ((str = in.readLine()) !=null)
{
StringTokenizer st =new StringTokenizer(str,":");
array[i][j] = str;
//System.out.println(array[i][j]); // this works and prints all the tokens to console iterating through each file
}
}
in.close();
}catch (IOException e){}
}
}
else{System.out.println("else statement");}}
private String[] columnNames ={"First Name","Surname","Gender","Ability",};//Column Names
private Object[][] data ={
{"Mary","Campione","Snowboarding",new Integer(5),new Boolean(false)},
{"Alison","Huml","Rowing",new Integer(3),new Boolean(true)},
{"Kathy","Walrath","Knitting",new Integer(2),new Boolean(false)},
{"Sharon","Zakhour","Speed reading",new Integer(20),new Boolean(true)}
};
publicint getColumnCount(){
return columnNames.length;
}
publicint getRowCount(){
return data.length;//change data to array
}
public String getColumnName(int col){
return columnNames[col];
}
public Object getValueAt(int row,int col){
return data[row][col];//change data to array
}
public Class getColumnClass(int c){
return getValueAt(0, c).getClass();
}
/*
* Don't need to implement this method unless your table's
* data can change.
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
*/
}

