how to write contents of an ArrayList to JTable
807591May 1 2008 — edited May 1 2008I have an ArrayList named 'innercell'. It contains the following contents
cs123, 0.34567
cs234, 0.5673
cs234,cs456, 0.5674
this arraylist is added to another arraylist 'cells'
cells.add(innercell);
now the contents of the 'cells' arraylist is
cs123, 0.34567
cs234, 0.5673
cs234,cs456, 0.5674
i have created a JTable and its variable name is 'jtable' (object) (*i'm using NetBeans IDE 6.0*)
and the model of this JTable is as below (it is the generated code by NetBeans IDE 6.0)
jtable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null}
},
new String [] {
"Title 1", "Title 2"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.String.class
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
});
i have only 2 columns in the table, and 9 rows
now i want 2 place contents of 'cells' arraylist to JTable as below
Tittle 1 Title 2
cs123 0.34567
cs234 0.5673
cs234,cs456 0.5674
to write in this form i'm using following code
String[] columnNames={"Title 1","Title 2"};
Object[][] rowdata=new Object[9][2];
str=items;//items is the string value.the possible 'items' values {cs123,...}
sup=value;//value is the double value. the possible 'value' are {0.5674,......}.i'm not given how i'm geting these values
for(int k=0;k<2;k++){
if(k==0)
rowdata[i][k]=str;
else
rowdata[i][k]=sup;
}
jtable=new JTable(rowdata,columnNames);//is this statement is right to add the contents of arraylist to the table?
jtable.setVisible(true);
but it is not working....i'm getting "NullPointerException"