2

Hey I was trying to get the values from a JTable to an array and then print it. I seems like it is actually taking the adress of something instead of taking the value. I don't understand why. Here is my code:

public Object[][] getTableData (JTable table) {
    DefaultTableModel dtm = (DefaultTableModel) table.getModel();
    int nRow = dtm.getRowCount();
    int nCol = dtm.getColumnCount();
    Object[][] tableData = new Object[nRow][nCol];
    for (int i = 0 ; i < nRow ; i++){
        for (int j = 0; j < nCol ; j++)
        tableData[i][j] = dtm.getValueAt(i,j);
    }
    System.out.println(Arrays.asList(tableData));
    return tableData;
}

3 Answers 3

3

DefaultTableModel model = new javax.swing.table.DefaultTableModel();

    model.addColumn("Col1");
    model.addColumn("Col2");

    model.addRow(new Object[]{"1", "v2"});
    model.addRow(new Object[]{"2", "v2"});

    List<String> numdata = new ArrayList<String>();
    for (int count = 0; count < model.getRowCount(); count++){
          numdata.add(model.getValueAt(count, 0).toString());
    }

    System.out.println(numdata);

try this

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

1 Comment

you can get a clue for your problem
0

you have to use System.out.println(Array.toString(//your code here));

5 Comments

otherwise as you've encountered it prints a memory location instead of the visual value
thanks but i still get the same thing; I get this "[[Ljava.lang.Object;@6f434304]"
well now that i think of it you have a println and a return method for Public Object try deleting the return method and see what happens (p.s it might help future awnserers if you include a bit of the main where you call public object
I did it but it does not work, I dont see how deleting the return would do anything since the println is just before it.
well I'm stumped now to my best guess right now is that your not calling this method correctly in the main try going through the main and checking for tiny logic errors like 1/0 or stuff like that.
0

List< Object> list = new ArrayList< Object>();

for (int i = 0 ; i < nRow ; i++){

    for (int j = 0; j < nCol ; j++)
     list.add(dtm.getValueAt(i,j).toString());  

}

1 Comment

i get a error as well: at components.CompleteListeFrame.getTableData(CompleteListeFrame.java:301)

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.