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

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

My arraylist only has one column, can i still do this, if so, how?
@Matt9Atkins please (stackoverflow.com/questions/9133523/…) are you joking
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);

1 Comment

Print value of list.get(Integer.parseInt( age[row].toString() )).?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.