使用新值计算JTable列中的总值

时间:2013-01-18 06:50:25

标签: java swing jtable

我想计算索引[0] [2]中的总值,前一个值为0,但我已经用计算结果“1 / y”替换并显示它,当我计算总值时,求和的值是以前的值,包含“0”,这是我的代码,

     //observation table
                                     //0             1        2    3      4      5     6                       
     titleColumn = new Object[]{"Time (Second)","Medicine", "1/y","x2", "X/Y", "Y^", "Error"};
                               //0   1    2   3   4   5   6
    allData = new Double[][]  {{1.0,1.02,0.0,0.0,0.0,0.0,0.0},
                               {2.0,0.667,0.0,0.0,0.0,0.0,0.0},
                               {3.0,0.367,0.0,0.0,0.0,0.0,0.0},
                               {4.0,0.278,0.0,0.0,0.0,0.0,0.0},
                               {5.0,0.237,0.0,0.0,0.0,0.0,0.0},
                               {6.0,0.187,0.0,0.0,0.0,0.0,0.0},
                               {7.0,0.155,0.0,0.0,0.0,0.0,0.0},
                               {8.0,0.156,0.0,0.0,0.0,0.0,0.0},
                               {9.0,0.142,0.0,0.0,0.0,0.0,0.0},
                               {10.0,0.111,0.0,0.0,0.0,0.0,0.0},
                               {11.0,0.12,0.0,0.0,0.0,0.0,0.0},
                               {12.0,0.097,0.0,0.0,0.0,0.0,0.0},
                               {13.0,0.099,0.0,0.0,0.0,0.0,0.0},
                               {14.0,0.089,0.0,0.0,0.0,0.0,0.0},
                               {15.0,0.079,0.0,0.0,0.0,0.0,0.0},
                               {0.0,0.0,0.0,0.0,0.0,0.0,0.0}};

    tableModelObservation = new DefaultTableModel(allData, titleColumn);
    tableObservation.setModel(tableModelObservation);
    int row,column,inputRow,inputColumn;

    //index [0][2] was replaced with calculation 1/y
    row = 0;
    column = 1;
    inputRow = 0;
    inputColumn = 2;
    double onePerY = 0;
    for(int a=0;a<allData.length;a++){
        onePerY = 1/allData[row][column];
        //is this the way to use getValueAt() and for indicating that the dependent cell has been change ?
        tableObservation.getModel().getValueAt(row, column);
        tableObservation.firePropertyChange("1/y", inputRow, inputColumn);
        tableObservation.getModel().setValueAt(onePerY, inputRow, inputColumn);  
        inputRow++;
        row++;
    }    

    //calculation total 1/y, summing is still using previous value "0"
    row = 0;
    column = 2;
    inputRow = 15;
    inputColumn = 2;
    double totalOnePerY = 0;
    for (int a=0;a<allData.length;a++){
        totalOnePerY += allData[row][column];
        row++;
    }
    //displaying result in row index[15] and column index[2] 
    tableObservation.getModel().setValueAt(totalOnePerY, inputRow, inputColumn);

此列在计算过程后输出值“1 / y” column 1/y

我应该怎么做才能用新值来总结它? 你提供的所有帮助,我将不胜感激,谢谢

2 个答案:

答案 0 :(得分:5)

DependentColumn所示,您可以在getValueAt()的实施中计算派生值。如果值所依赖的列是可编辑的,请确保触发指示依赖单元格已更改的事件。

附录:我已经使用 getValueAt()添加了我的代码,但结果是一样的。

我猜不到你的sscce:我发现你在之后访问了allData数组,用它来构建和更新DefaultTableModel。我不确定练习的目标,但你可能不会在之前更新数组DefaultTableModel在内部使用Vector,并在构造完成后忽略该数组。

答案 1 :(得分:2)

您可以尝试使用包裹原始模型的EnvelopeTableMode并提供分组功能(SUM,COUNT,AVG ...)。