-1

Possible Duplicate:
Is there any way by which I can create a JTable using the data in an ArrayList?

The code of my program is as follows:

//A loan Amortization example
public class LoanTable 
{

public static void main(String[] args) 
{
    double p,n,r,a,rb,ia,pa,pno=1;

    //creating a double arraylist
    List<double[]> l = new ArrayList<double[]>();

    p=250000;
    n=360;
    r=0.416/100;
    a=1342.05;

    rb=p;

//calculating the amount as interest, amount as principal and remaining balance 
    for(int i=1; i<=n;i++)
    {
        ia=(rb*r);
        pa=a-ia;
        rb=rb-pa;

        l.add(new double[] { pno,a,ia,pa,rb });

        pno++;

    }

    for (double[] row : l) 
    {
        System.out.println(Arrays.toString(row));

    }

}
}

How do I import the values in the rows to create a table? It becomes difficult to import the values.

The output is somewhat like :

[1.0, 1342.05, 1040.0, 302.04999999999995, 249697.95]

[2.0, 1342.05, 1038.7434719999999, 303.30652800000007, 249394.64347200003]

[3.0, 1342.05, 1037.48171684352, 304.56828315648, 249090.07518884353]

[4.0, 1342.05, 1036.214712785589, 305.835287214411, 248784.23990162913]

[5.0, 1342.05, 1034.942437990777, 307.1075620092229, 248477.13233961992]

[6.0, 1342.05, 1033.6648705328187, 308.3851294671813, 248168.74721015274]

[7.0, 1342.05, 1032.3819883942353, 309.6680116057646, 247859.07919854697]

[8.0, 1342.05, 1031.0937694659553, 310.9562305340446, 247548.12296801293]

[9.0, 1342.05, 1029.8001915469338, 312.2498084530662, 247235.87315955988]

[10.0, 1342.05, 1028.501232343769, 313.5487676562309, 246922.32439190365]

Thanks.

Community
  • 1
  • 1
Maddy
  • 167
  • 1
  • 3
  • 12
  • What is your expected output?? How do you want to display them? – Rohit Jain Oct 14 '12 at 19:20
  • I want the output in a JTable with the data in this arraylist... – Maddy Oct 14 '12 at 19:22
  • But I don't see any JTable here.. What have you tried so far? – Rohit Jain Oct 14 '12 at 19:25
  • you have to implement arraydriven model – Roman C Oct 14 '12 at 19:27
  • Thats the problem. I dod not know how to import all this data into a JTable. A JTable has a fixed number of rows and columns. Any idea how I could import it? As of now, I have created this arraylist which displays the data. But it would be better, if I could display this data in a JTable. – Maddy Oct 14 '12 at 19:28

2 Answers2

1

One approach would be to use the List<double[]> as backing data in an AbstractTableModel. There are plenty of good examples of this table model available online.

Community
  • 1
  • 1
Reimeus
  • 158,255
  • 15
  • 216
  • 276
1

The crucial thing to understand while working with JTable is that every JTable is internally backed by a TableModel. If you're new to using tables, I would suggest "How to Use Tables" as a good place to start learning about the implementation

Every table model basically implements the TableModel interface. For your scenario, you should look at creating your own table model, extending the AbstractTableModel class. As per the javadoc,

To create a concrete TableModel as a subclass of AbstractTableModel you need only provide implementations for the following three methods:

public int getRowCount();
public int getColumnCount();
public Object getValueAt(int row, int column);

So your implementation of TableModel will be minimally something like this [note that I said minimally because there're other methods of this class that you would also want to override, for example getColumnNames(...)]:

import java.util.List;  
import javax.swing.table.AbstractTableModel;

public class MyTableModel extends AbstractTableModel {
    
    private List<double[]> dataList;
    
    public MyTableModel(List<double[]> dataList){
        this.dataList = dataList;
    }

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

    @Override
    public int getColumnCount() {
        return (dataList.size() > 0) ? dataList.get(0).length : 0;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        
        if(rowIndex < dataList.size()){
            double[] columns = dataList.get(rowIndex);
            if(columns.length > columnIndex){
                return columns[columnIndex];
            }
        }
        return null;
    }
}
Community
  • 1
  • 1
Sujay
  • 6,753
  • 2
  • 30
  • 49