0

I have an arraylist and each element of the arrayList contains a small arrayList of data.

I need to create a table from this data. What would you recommend being the best route to take with solving this?

Thanks in advance for any help

2
  • Do you want to display a textual table or a GUI table? Commented Feb 7, 2013 at 14:28
  • What does the inner list contain? Does it contain objects? Commented Feb 7, 2013 at 14:36

3 Answers 3

3

See How to create a Table Model

A small example showing how to create a Model and store your objects in the model. Any operation on the data should be done using the model.

Create a model and use a list to store the data for that table. As shown in the below example. Each of the row in the table is a Student object and each object is stored in the list. Traverse the list and get each object and show in the table. This is done using getValueAt(..) method.

JTable Model Example

import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;


public class JTableList {
    public static void main(String[] args) {

        Runnable r = new Runnable() {

            @Override
            public void run() {
                CustomModel model = new CustomModel();
                JTable table = new JTable();
                table.setModel(model);

                JFrame frame = new JFrame();
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);             
            }
        };

        EventQueue.invokeLater(r);
    }

}

class CustomModel extends AbstractTableModel {

    List<Student> data;
    String[] columnNames = {"Name", "Age"};

    public CustomModel() {
        data = new ArrayList<Student>();

        data.add(new Student("Amar", 1));
        data.add(new Student("Sam", 2));
        data.add(new Student("Amar", 1));
        data.add(new Student("Sam", 2));
        data.add(new Student("Amar", 1));
        data.add(new Student("Sam", 2));


    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public int getColumnCount() {
        return 2;
    }

    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Student student = data.get(rowIndex);
        switch (columnIndex) {
        case 0:
            return student.getName();
        case 1:
            return student.getAge();
        default:
            return null;
        }
    }

}

class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

To display a GUI table of a List of lists, I would create a class that implements the 'TableModel' interface, and has a constructor that takes in your 'List>'

public class ListTableModel <T> implements TableModel {

    private List<List<T>> source;

    public ListTableModel(List<List<T>> source) {

        this.source = source;

    }


    //Override 'getRowCount' 
    //The row count would be calculated as the size of the outer list.
    @Override
    public int getRowCount() {
        return source.size();
    }

    //Override 'getColumnCount'
    //The column count would be calculated as the max size of the inner lists
    @Override
    public int getColumnCount() {
        int max = 0;
        for(List<T> row : source) {
            max = Math.max(max, row.size());
        }
        return max;
    }

    //Override 'getColumnName'
    //Lets go ahead and just give a unique name to each column based on the index.
    //This could be populated from an input taken by the constructor, but we
    //won't worry about that now.
    @Override
    public String getColumnName(int columnIndex) {
        return "Column " + columnIndex;
    }

    //Override 'getColumnClass'
    //The class would technically be the generic type 'T', so to get this we 
    //will simply just get the calss of the first element.
    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return source.get(0).get(0).getClass();
    }


    //Override 'isCellEditable'
    //I'm going to assume we don't want cell to be editable.
    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        List<T> row = source.get(rowIndex);
        if(columnIndex >= row.size())
            return null;
        else 
            return row.get(columnIndex);
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        //required but we will assume that you cannot change the source list
        //if we needed to, it wouldn't be too difficult to implement.
    }

    @Override
    public void addTableModelListener(TableModelListener l) {
        //required but not used (will only be used if the source could change)
    }

    @Override
    public void removeTableModelListener(TableModelListener l) {
        //required but not used (will only be used if the source could change)
    }

}

you would use this model with your list like so:

List<List<String>> myList = new ArrayList();

//Populate 'myList'...

JTable table = new JTable();

//Add table to view...

table.setModel(new ListTableModel(myList));

EDIT I can show you how to implement TableModel if you would like.

3 Comments

That would be great if you could give us a quick tutorial on how to implement TableModel! Thanks a lot.
Perfect! Thanks very much, i'll give it a go. I Really aprreciate the help
@user2029412 I changed the code to implement TableModel. This is a very basic implementation and I would recommend looking at Che's example of creating a Table Model. By the way, the great thing about creating a class like this, is that you can reuse it in any of your projects!
1

The List Table Model can be used to do this.

It is a more complex and flexible solution implementing the earlier suggestions given in this thread.

Comments

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.