Hi!
I've spent 4 hours trying to read the data from file to ArrayList and then to JTable, but still cannot solve one problem.
I have the following class UserDataRow, where the data about a user is described.
public class UserDataRow {
public String regnumber;
public String firstname;
public String lastname;
public String profession;
public String sex;
public String dateofbirth;
public int size;
public UserDataRow(String rn, String fn, String ln, String p, String s, String dob) {
this.regnumber = rn;
this.firstname = fn;
this.lastname = ln;
this.profession = p;
this.sex = s;
this.dateofbirth = dob;
this.size = 6;
}
}
I'm using the following code to read the data from file:
public void readuserdatafromfile() throws IOException {
try{
FileInputStream fstream = new FileInputStream("src/filetree/UserDataFile.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine = "";
String[] tokens = strLine.split(", ");
//Read file line by line
//int row = 0;
UserDataRow p;
while ((strLine = br.readLine()) != null) {
// Copy the content into the array
tokens = strLine.split(", ");
userdatarow.add(new UserDataRow(tokens[1], tokens[2], tokens[3], tokens[4], tokens[5], tokens[6]));
listofusers.add(userdatarow);
}
//Close the input stream
in.close();
} catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
Now I want just to put this 2d array "listofusers" into my JTable:
MyTableModel aModel = new MyTableModel(listofusers); // This constructor must add data to JTable, however I don't know how to implement it.
filetree.Form.tableDetails.setModel(aModel);
As I understand, the problem is with MyTableModel:
class MyTableModel extends DefaultTableModel{
private ArrayList<ArrayList<UserDataRow>> listofusers = null;
public MyTableModel(ArrayList<ArrayList<UserDataRow>> listofusers){
this.listofusers=listofusers;
setColumnIdentifiers(new String [] {"Reg.Nr.",
"Name",
"Surename",
"Profession",
"Sex",
"Date of Birth"});
}
@Override
public Object getValueAt(int row, int column){
if(row>=listofusers.size()){
return null;
} else return listofusers;
}
}
So, how could I implement "addAll" method or something like this in MyTableModel?
Pliz post any advises or links to good tutorials!