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);

}

*/

}

[7393 byte] By [JasonDaintera] at [2007-10-2 18:26:19]
# 1
Also, im willing to pay some sort of reward is someone can help me to solve this!! The deadline is soon ending so please HELP!!
JasonDaintera at 2007-7-13 19:47:25 > top of Java-index,Java Essentials,Java Programming...
# 2
Why not do all of the loading in seperate method.[code]public loadFileData() { // Your code }and simply load the data from file directly into Object[][] data?
newbie85a at 2007-7-13 19:47:25 > top of Java-index,Java Essentials,Java Programming...
# 3

Thanks for the reply,

I could do that... but would it help? The current code is running as the constructor so I dont see how moving it to a different method would improve this, unless im missing something?!

When uncomment the line:

System.out.println(array[j]);

When i run the code the output is encouraging displaying:

First Name:bfdfdf

Surname:wsfwef

Sex:Male

Ability:Medium

Topic:

Exercise:

First Name:Fred

Surname:Brown

Sex:Male

Ability:Low

Topic:

Exercise:

First Name:Jon

Surname:Doe

Sex:Female

Ability:High

Topic:

Exercise:

...etc

This array is clearly being filled and its iterating ok.... (I will worry about take out the lables later)...

Any other ideas?

JasonDaintera at 2007-7-13 19:47:25 > top of Java-index,Java Essentials,Java Programming...
# 4

Also..

I think you are mistaken in what i want to do... I would just load the file into an array but i have lots of different text files that i need to iterate through and load each one into the JTable...

This whole code is to allow me to save player details so that I can update the file to allow me to save the game after an exercise is complete.

JasonDaintera at 2007-7-13 19:47:25 > top of Java-index,Java Essentials,Java Programming...
# 5

In the future, Swing related questions should be posted in the Swing forum.

> When i run the code the output is encouraging displaying:

Yes it is because reading each file is the most complicated part of your overall processing.

Now to make life simple for yourself, just use the DefaultTableModel to store the data. Don't try to create your own TableModel. For example you don't know how many files exist so you don't know how big to make your data array. The DefaultTableModel allows you to add rows dynamically.

Here is an example that is very similiar to yours. Each row in the file represents a row in the table. I parse each row to get the column information then add the row to the table.

In your case each file represents a row and each row in the file represents a column. So you should be able to build a row Vector with only slight modifications to your code.

Replies 1 and 4:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=315172

camickra at 2007-7-13 19:47:25 > top of Java-index,Java Essentials,Java Programming...
# 6
i HAVE MOVED THIS POST TO THE SWING SECTION... I MANAGED TO FIND A SOLUTION BUT NOW HAVE ANOTHER PROBLEM. William to give a fnancial reward to anyone who can help me solve this, my deadline is tomorrow!! http://forum.java.sun.com/thread.jspa?threadID=730624
JasonDaintera at 2007-7-13 19:47:25 > top of Java-index,Java Essentials,Java Programming...