M9A
M9A

Reputation: 3286

How to update JTable using values from an array in java

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]);  
    }
}

Upvotes: 1

Views: 1209

Answers (2)

mKorbel
mKorbel

Reputation: 109823

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

Upvotes: 1

RanRag
RanRag

Reputation: 49597

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);

Upvotes: 0

Related Questions