1

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]);  
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
M9A
  • 3,168
  • 14
  • 51
  • 79

2 Answers2

1

1) ArrayList in the AbstractTableModel returns Column, please read tutorial about JTable how TableModel works

2) you can change ArrayList<Integer> to the Vector<Vector<Integer>> or Interger[][], then you don't need to define for AbstractTableModel, only use default contructor for JTable

JTable(Object[][] rowData, Object[] columnNames)

or

JTable(Vector rowData, Vector columnNames)

3) add Integer value to the DefaultTableModel

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • My arraylist only has one column, can i still do this, if so, how? – M9A Feb 05 '12 at 20:57
  • @Matt9Atkins please (http://stackoverflow.com/questions/9133523/use-arraylist-to-fill-a-jtable-in-java) are you joking – mKorbel Feb 05 '12 at 21:21
0

list.get(age[row]); requires list.get(int) whereas age[row] is object.

So, try this

int i =Integer.parseInt( age[row].toString() ); 

and than

 list.get(i);
RanRag
  • 48,359
  • 38
  • 114
  • 167