Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
792 views
in Technique[技术] by (71.8m points)

java - Calculating total value in JTable Column using new value

I want to calculate total value in index [0][2], previous value is 0, but I have replaced and display it with the calculation result “1/y”, and when I calculate total value, value that summed is previous value that containing “0”, this my code,

     //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);

this column values ”1/y” after calculation process column 1/y

What should I do, to be able to summing it using new value ? all the assistance that you gave, I would appreciate it, thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As shown in DependentColumn, you can calculate derived values in your implementation of getValueAt(). If a column on which the value depends is editable, be sure to fire an event indicating that the dependent cell has changed.

Addendum: I’ve added my code with getValueAt(), but the result is the same.

Absent your sscce I'm guessing: I see that you access the allData array after you use it to construct and update the DefaultTableModel. I'm not sure about the goal of the exercise, but you probably wan't to update the array before doing so. DefaultTableModel uses Vector internally, and it ignores the array after construction completes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...