Java JTable Column sumColumTable(JTable pTable, int pColumna)

Here you can find the source of sumColumTable(JTable pTable, int pColumna)

Description

Realiza la Suma de toda una Colunma de un Jtable y lo retorna como String con formato

License

Open Source License

Declaration


public static double sumColumTable(JTable pTable, int pColumna) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import javax.swing.*;

import java.text.ParseException;

public class Main {
    /**//from  w  ww.j a  v  a2s .c  o m
     * Realiza la Suma de toda una Colunma de un Jtable y lo retorna como String con formato
     */

    public static double sumColumTable(JTable pTable, int pColumna) {
        double suma = 0.00;
        for (int p = 0; p < pTable.getRowCount(); p++) {
            suma += getDecimalNumber(((String) pTable.getValueAt(p,
                    pColumna)).trim());
            suma = getDecimalNumber(formatNumber(suma));
        }
        return suma;
    }

    public static double getDecimalNumber(String pDecimal) {
        double decimalNumber = 0.00;
        try {
            java.text.DecimalFormat formatDecimal = new java.text.DecimalFormat(
                    "###,###,##0.00");
            java.text.DecimalFormatSymbols symbols = new java.text.DecimalFormatSymbols();
            symbols.setGroupingSeparator(',');
            symbols.setDecimalSeparator('.');
            formatDecimal.setDecimalFormatSymbols(symbols);
            decimalNumber = formatDecimal.parse(pDecimal).doubleValue();
        } catch (ParseException errParse) {
            errParse.printStackTrace();
        }
        return decimalNumber;
    }

    public static double getDecimalNumber(String pDecimal, int pDecimales) {
        double decimalNumber = 0.00;
        try {
            java.text.DecimalFormat formatDecimal = new java.text.DecimalFormat(
                    "###,##0." + caracterIzquierda("", pDecimales, "0"));
            java.text.DecimalFormatSymbols symbols = new java.text.DecimalFormatSymbols();

            symbols.setGroupingSeparator(',');
            symbols.setDecimalSeparator('.');
            formatDecimal.setDecimalFormatSymbols(symbols);
            decimalNumber = formatDecimal.parse(pDecimal).doubleValue();
        } catch (ParseException errParse) {
            errParse.printStackTrace();
        }
        return decimalNumber;
    }

    public static String formatNumber(double pValue) {
        java.text.DecimalFormat formatDecimal = new java.text.DecimalFormat(
                ",##0.00");
        java.text.DecimalFormatSymbols symbols = new java.text.DecimalFormatSymbols();
        symbols.setGroupingSeparator(',');
        symbols.setDecimalSeparator('.');
        formatDecimal.setDecimalFormatSymbols(symbols);
        return formatDecimal.format(pValue).toString();
    }

    public static String formatNumber(double pValue, int pDecimales) {
        java.text.DecimalFormat formatDecimal = new java.text.DecimalFormat(
                ",##0." + caracterIzquierda("", pDecimales, "0"));
        java.text.DecimalFormatSymbols symbols = new java.text.DecimalFormatSymbols();
        symbols.setGroupingSeparator(',');
        symbols.setDecimalSeparator('.');
        formatDecimal.setDecimalFormatSymbols(symbols);
        return formatDecimal.format(pValue).toString();
    }

    public static String formatNumber(double pValue, String pFormatNumber) {
        java.text.DecimalFormat formatDecimal = new java.text.DecimalFormat(
                pFormatNumber);
        java.text.DecimalFormatSymbols symbols = new java.text.DecimalFormatSymbols();
        symbols.setGroupingSeparator(',');
        symbols.setDecimalSeparator('.');
        formatDecimal.setDecimalFormatSymbols(symbols);
        return formatDecimal.format(pValue).toString();
    }

    public static String caracterIzquierda(int parmint, int parmLen,
            String parmCaracter) {
        return caracterIzquierda(String.valueOf(parmint), parmLen,
                parmCaracter);
    }

    public static String caracterIzquierda(long parmint, int parmLen,
            String parmCaracter) {
        return caracterIzquierda(String.valueOf(parmint), parmLen,
                parmCaracter);
    }

    public static String caracterIzquierda(double parmint, int parmLen,
            String parmCaracter) {
        return caracterIzquierda(String.valueOf(parmint), parmLen,
                parmCaracter);
    }

    public static String caracterIzquierda(String parmString, int parmLen,
            String parmCaracter) {

        String tempString = parmString;

        if (tempString.length() > parmLen)
            tempString = tempString.substring(
                    tempString.length() - parmLen, tempString.length());
        else {
            while (tempString.length() < parmLen)
                tempString = parmCaracter + tempString;
        }

        return tempString;

    }
}

Related

  1. setColumnOrder(final JTable table, final String positions)
  2. setearPrimerRegistro(JTable pTabla, JTextField pTextoDeBusqueda, int pColumna)
  3. setearTextoDeBusqueda(JTable pTabla, JTextField pTextoDeBusqueda, int pColumna)
  4. setSelectedRow(JTable table, int rowIndex, int columnIndex)
  5. setupColumns(JTable table)