用Java中的计算结果的值替换JTable的值

时间:2013-01-17 16:40:47

标签: java swing jtable defaulttablemodel

我想在index [0] [2]中替换变量数组的值,即coloumn title的名称是“1 / y”。以前的值是0.0,我想用计算结果的值替换它,但是当我尝试显示它时,它的值仍然是0.0,这是我的代码

titleColoumn = 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},
                                    {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}};

    tableObservation = new DefaultTableModel(allData, titleColoumn);
    table.setModel(tableObservation);

    int row,coloumn;
    //calculation 1/y
    row = 0;
    coloumn = 1;
    int inputRow = 0;
    int inputColoumn = 2;
    double onePerY;
    for(int a=0;a<allData.length;a++){
        onePerY = 1/allData[row][coloumn];
        //replace value
        allData [inputRow][inputColoumn] = onePerY;
        inputRow++;
        row++;
        System.out.println(onePerY);
    }    

我应该怎么做才能更换它?你提供的所有帮助,我将不胜感激,谢谢

2 个答案:

答案 0 :(得分:2)

您更新的数组值不是TableModel值。使用jTable.getModel().setValueAt()传递inputRow,inputColoumn和适当的值。您的模型必须是可编辑的。如果你使用DefaultTableModel,默认情况下它是可编辑的。

答案 1 :(得分:2)

了解DefaultTableModel并使用它来存储表格的数据。如果数据发生变化,您必须在表格模型中更新。

另请查看How to Use Tables