Package Controls

Source Code of Controls.SpentTable

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package Controls;

import Utils.Profile;
import java.text.DecimalFormat;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableColumn;

/**
*
* @author User
*/
public class SpentTable extends JTable
{
    private NonEditableDefaultTableModel tmSpent;
    private DecimalFormat nfDecimal;
    private DefaultTableColumnModel dtcColumns;
    public SpentTable()
    {
        super();
        tmSpent=new NonEditableDefaultTableModel();
        super.setModel(tmSpent);
        super.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        //this.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
        dtcColumns=new DefaultTableColumnModel();
        super.setColumnModel(dtcColumns);
        nfDecimal=new DecimalFormat("##0.00");
    }
    public void setData(String[] strGroups, double[][] data, String[] strDates)
    {
        //Regroup the arrays so that group "Other" will be the last one
        for(int i=0; i<(strGroups.length-1); i++)
        {
            if(strGroups[i].equals(Profile.OTHER))
            {
                double[] dblTempTransactions=data[i].clone();
                data[i]=data[data.length-1].clone();
                data[data.length-1]=dblTempTransactions;
                strGroups[i]=new String(strGroups[strGroups.length-1]);
                strGroups[strGroups.length-1]=Profile.OTHER;
            }
        }
        tmSpent.addColumn("");
        for(int i=0; i<strGroups.length; i++)
        {
            tmSpent.addColumn(strGroups[i]);
        }
        tmSpent.addColumn("Total income");
        Vector vctLastRow=new Vector();
        vctLastRow.add("Average");
        for(int i=0; i<strDates.length; i++)
        {
            Vector vctTemp=new Vector();
            vctTemp.add(strDates[i]);
            tmSpent.addRow(vctTemp);
        }
        double dblTotalIncome=0;
        double dblIncome=0;
        for(int i=0; i<data.length; i++)
        {
            double dblTotal=0;

            for(int j=0; j<data[i].length; j++)
            {
                //data[group number][transaction inside group]
                if(data[i][j]!=0)
                    tmSpent.setValueAt(nfDecimal.format(-data[i][j]), j, i+1);
                else
                    tmSpent.setValueAt(nfDecimal.format(data[i][j]), j, i+1);

                dblTotal-=data[i][j];
                if(j==data[i].length-1)
                {
                    vctLastRow.add(nfDecimal.format(dblTotal/data[i].length));
                }
            }
        }
        for(int z=0; z<data[0].length; z++)
        {
            for(int k=0; k<data.length; k++)
                    dblIncome-=data[k][z];
            tmSpent.setValueAt(nfDecimal.format(dblIncome), z, tmSpent.getColumnCount()-1);
            dblTotalIncome+=dblIncome;
            dblIncome=0;
            if(z==data[0].length-1)
            {
                vctLastRow.add(nfDecimal.format(dblTotalIncome/data[0].length));
            }
        }
        tmSpent.addRow(vctLastRow);
        TableColumn column=null;
        for (int i = 0; i < tmSpent.getColumnCount(); i++)
        {
            column = this.getColumnModel().getColumn(i);
            int intMaxString=this.getFontMetrics(this.getFont()).stringWidth(this.getColumnName(i));
            for(int l=0; l<this.getRowCount(); l++)
            {
                intMaxString=Math.max(intMaxString, this.getFontMetrics(this.getFont()).stringWidth((String)this.getValueAt(l, i)));
            }
            column.setPreferredWidth(intMaxString+10);
            intMaxString=0;
        }
    }


}
TOP

Related Classes of Controls.SpentTable

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.