0
public JTable(Object rowData[][], Object columnNames[])
Object rowData[][] = { { "Row1-Column1", "Row1-Column2", "Row1-Column3"},
                       { "Row2-Column1", "Row2-Column2", "Row2-Column3"} };
Object columnNames[] = { "Column One", "Column Two", "Column Three"};
JTable table = new JTable(rowData, columnNames);

I was wondering if we could use linked list instead.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
OnTheFly
  • 2,059
  • 5
  • 26
  • 61
  • 1
    Have you looked at the [javadoc](http://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html)? – Paul Samsotha Jan 08 '14 at 03:08
  • 4
    This question appears to be off-topic because it is about a matter that is better answered by referring to the [API documentation](http://docs.oracle.com/javase/7/docs/api/). – Andrew Thompson Jan 08 '14 at 03:16
  • 1
    This is a legitimate valid question. `DefaultTableModel` only takes an array and the outdated & deprecated `Vector`. The poster has a `List` and wants to make a table out of it. It's not necessarily clear from the JavaDocs how to accomplish this. – Steve Kuo Jan 08 '14 at 03:35

2 Answers2

2

Here is an example where a List<List<String>> is used instead of an Object[][]. I used a method to create a DefaultTableModel and set the model to the JTable

public DefaultTableModel createModel(List<List<String>> list, String[] columnNames) {

    DefaultTableModel model = new DefaultTableModel(columnNames, 0);
    for (List<String> row : list) {
        model.addRow(row.toArray());
    }

    return model;
}

See DefaultTableModel javadoc

Also see this answer where I used a Stack


Complete Running program:

import java.awt.BorderLayout;
import java.util.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class TestTable {

    private String[] colNames = { "Col 1", "Col2", "Col3", "Col4", "Col5" };

    private DefaultTableModel model;
    private JTable table;
    private MyStack myStack = new MyStack();
    private List<List<String>> list;


    public TestTable() {
        list = myStack.getList();

        model = createModel(list, colNames);
        table = new JTable(model);


        JFrame frame = new JFrame();
        frame.add(new JScrollPane(table), BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public DefaultTableModel createModel(List<List<String>> list, String[] columnNames) {

        DefaultTableModel model = new DefaultTableModel(columnNames, 0);
        for (List<String> row : list) {
            model.addRow(row.toArray());
        }

        return model;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new TestTable();
            }
        });
    }
}

class MyStack {
    public List<List<String>> list = new ArrayList<List<String>>();

    public MyStack() {
        int k = 1;
        for (int i = 0; i < 20; i++) {
            List<String> innerList= new ArrayList<String>();
            for (int j = 0; j < 5; j++) {
                innerList.add(String.valueOf( i * k * (j + 1)));
            }
            k++;
            list.add(innerList);
        }
    }

    public List<List<String>> getList() {
        return list;
    }
}
Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
1

According to Java Docs, it may accept Vectors as well: http://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html

Plus note you can potentially convert linkedList to a single-dimensional array like yourLinkedList.toArray() and then copy it to a two-dimensional array using a single loop (or nested loops if you like).

3yakuya
  • 2,622
  • 4
  • 25
  • 40