Hi I converted my arraylist into an array so I can use it to display its elements in a JTable but nothing is displaying. It is giving me an error (error is explained in code comments). I just want to have one column only which displays values from this array. Can someone guide me in the correct direction? Thanks
Here is my code:
private static class EnvDataModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;
private static ArrayList<Integer> list = new ArrayList<Integer>();
private Object age[];
…
public EnvDataModel() {
age=list.toArray();
}
public String getColumnName(int col) {
return "Age";
}
public int getColumnCount() {
return 1;
}
public int getRowCount() {
return list.size();
}
public Object getValueAt(int row, int col) {
// Error message The method get(int) in the type ArrayList<Integer> is not applicable for the arguments (Object)
return list.get(age[row]);
}
}
1)
ArrayListin theAbstractTableModelreturns Column, please read tutorial aboutJTablehow TableModel works2) you can change
ArrayList<Integer>to theVector<Vector<Integer>>orInterger[][], then you don’t need to define forAbstractTableModel, only use default contructor forJTableJTable(Object[][] rowData, Object[] columnNames)
or
JTable(Vector rowData, Vector columnNames)
3) add
Integervalue to the DefaultTableModel