How to show ArrayList values in JTable

emsysindia
emsysindia used Ask the Experts™
on
hi,
   I have an ArrayList, I need to show ArrayList values in a tabular form through JTable or any TableModel class.
How can I do it..

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Kamaraj SubramanianApplication Support Analyst

Commented:
check this thread

http://forums.sun.com/thread.jspa?threadID=5368192

you can only load vector or object to jtable.

so, you need some conversion here
Top Expert 2016
Commented:
Make a TableModel out of your List
Top Expert 2016

Commented:
How do expect the rows and columns to be laid out?
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

check this example, it uses Vector, all you need to do is replace that with ArrayList and change method names, for exemple,'

vector.addElement will be replaced with arraylist.add
Take table row as an one data object, then whole table can be taken as list of rows.That way you can implement tablemodel useing an aArrayList.

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

public class ArrayListTableModel extends AbstractTableModel
{
  private List<MyTableRowData> data = new ArrayList<MyTableRowData>();
  private String[] columns = new String[] { "Column1", "Column2", "Column3" };

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

  public int getColumnCount()
  {
    return columns.length;
  }

  public Object getValueAt(int rowIndex, int columnIndex)
  {
    MyTableRowData tempData = data.get(rowIndex);
    if (columnIndex == 0)
    {
      tempData.getColumn1Data();
    }
    else if (columnIndex == 1)
    {
      tempData.getColumn2Data();
    }
    else if (columnIndex == 2)
    {
      tempData.getColumn3Data();
    }
    return "";
  }

  class MyTableRowData
  {
    private String column1Data;
    private String column2Data;
    private String column3Data;

    public MyTableRowData()
    {

    }

    public MyTableRowData(String column1Data, String column2Data, String column3Data)
    {
      this.column1Data = column1Data;
      this.column2Data = column2Data;
      this.column3Data = column3Data;
    }

    public String getColumn1Data()
    {
      return column1Data;
    }

    public void setColumn1Data(String column1Data)
    {
      this.column1Data = column1Data;
    }

    public String getColumn2Data()
    {
      return column2Data;
    }

    public void setColumn2Data(String column2Data)
    {
      this.column2Data = column2Data;
    }

    public String getColumn3Data()
    {
      return column3Data;
    }

    public void setColumn3Data(String column3Data)
    {
      this.column3Data = column3Data;
    }

  }
}

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial