I'm having an issue with adding new data to a table row. Every row I add contains the same data which is always the last data I grabbed from my database. I'm not sure if my issue has to do with how I set up the data to be passed or the table itself or both... Any help would be appreciated. It seems like the tablemodel is holding the memory spot ArrayList I'm passing. If I have to set up an arraylist of arraylists, to pass how do I do that?
My output is always:
1,4,Laser,10,120,100
1,4,Laser,10,120,100
1,4,Laser,10,120,100
1,4,Laser,10,120,100
Desired output:
1,1,Beam,10,99,100
1,2,Canon,10,120,100
1,3,Missile,10,66,100
1,4,Laser,10,120,100
/*
* Extract weaponIDs by hullType from weapon database
*/
private void setWeapons(int hullType){
// equpModel is the tableModel
equipModel.clearTable();
Weapon tempWeapon = new Weapon();
ArrayList newData = new ArrayList();
for (Iterator <Weapon> i = dataBaseManager.weaponList.iterator(); i.hasNext(); ){
tempWeapon = i.next();
if (tempWeapon.weaponClass == hullType){
newData.add(0,1);
newData.add(1,tempWeapon.weaponID);
newData.add(2,tempWeapon.weaponName);
newData.add(3,tempWeapon.weaponCps);
newData.add(4,tempWeapon.weaponMass);
newData.add(5,tempWeapon.weaponCost);
equipModel.insertRow(newData);
}
}
}
Here is a snipet from the table class
public class GenTableModel extends AbstractTableModel {
private ArrayList data; // Holds the table data
private String[] columnNames; // Holds the column names.
/**
* Constructor: Initializes the table structure, including number of columns
* and column headings. Also initializes table data with default values.
*
* @param columnscolumns[] array of column titles.
* @param defaultvdefaultv array of default value objects, for each column.
* @param rowsrows number of rows initially.
*/
public GenTableModel(String[] columns, Object[] defaultv, int rows) {
// Initialize number of columns and column headings
columnNames = new String[ columns.length ];
for(int i = 0; i < columns.length; i++) {
columnNames [ i ] = new String(columns [ i ]);
}
// Instantiate Data ArrayList, and fill it up with default values
data = new ArrayList();
for(int i = 0; i < rows; i++) {
ArrayList cols = new ArrayList();
for(int j = 0; j < columns.length; j++) {
cols.add(defaultv [ j ]);
}
data.add(cols);
}
}
/**
* Adds a new row to the table.
*
* @param newrowArrayList new row data
*/
public void insertRow(ArrayList newrow) {
data.add(newrow);
super.fireTableDataChanged();
}
/**
* Clears the table data.
*/
public void clearTable() {
data = new ArrayList();
super.fireTableDataChanged();
}
}