方案:
JTable中包含以下数据,我试图来形容我想在下面的图片做: - JTable中错误的列求和值
所以,我想我能够解释我在这里想要达到的目标。
问题 不显示准确的结果(总和),ofcourse。我已经使用的代码是:
public void docTotal_Income(){
try{
int totC=8,xC=3,lC=4,eC=5, sC=6; // totC is the last column, xC-3rd, lC-4th and so on...
for(int i=0;i<(easypath.doctorBusiness_table.getRowCount());i++){ // "easypath.doctorBusiness_table" is the table name
sumTot += Double.parseDouble(easypath.doctorBusiness_table.getModel().getValueAt(i, totC).toString());
sumTotx += Double.parseDouble(easypath.doctorBusiness_table.getModel().getValueAt(i, xC).toString());
sumTotl += Double.parseDouble(easypath.doctorBusiness_table.getModel().getValueAt(i, lC).toString());
sumTote += Double.parseDouble(easypath.doctorBusiness_table.getModel().getValueAt(i, eC).toString());
sumTots += Double.parseDouble(easypath.doctorBusiness_table.getModel().getValueAt(i, sC).toString());
}
easypath.totalEarnt_docBus_tf.setText(String.valueOf(sumTot));
easypath.xTotIncome_tf.setText(String.valueOf(sumTotx));
easypath.lTotIncome_tf.setText(String.valueOf(sumTotl));
easypath.eTotIncome_tf.setText(String.valueOf(sumTote));
easypath.sTotIncome_tf.setText(String.valueOf(sumTots));
sumTot = 0; // public static
sumTotx = 0; // values globally
sumTotl = 0; // declared
sumTote = 0; // and
sumTots = 0; // initialised 0
}
catch(Exception ex){
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "Error in totalling income");
}
}
我与Document Listener
炼JTable
后调用该方法docTotal_Income()
(工作正常),最后射击的JButton
的eventListener
。
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
new doctor().docTotal_Income(); // doctor is the class
}
毕竟这些我得到不规则的总和。我猜测我在某个地方出现了逻辑错误,但是我还有其他什么东西丢失了吗?
我很乐意欣赏这方面的任何建议。谢谢你的时间
什么是“非正规求和”?你的总分是0.01吗?减去1,000,000?总数是[NaN](http://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#NaN)?无穷? – VGR
不规则并不意味着大小或以往的任何错误。我试图解释它给出了错误的求和,5 + 5 = 20等等......我认为这是我的一个**逻辑错误**,但我无法指出它,所以把整个场景放入这个订单 – mustangDC
把数字放到XxxTableModel中,覆盖模型定义里面的getColumnClass – mKorbel