2015-07-10 57 views
0

方案
JTable中包含以下数据,我试图来形容我想在下面的图片做: - enter image description hereJTable中错误的列求和值

所以,我想我能够解释我在这里想要达到的目标。

问题 不显示准确的结果(总和),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 ListenerJTable后调用该方法docTotal_Income()工作正常),最后射击的JButtoneventListener

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {           
    new doctor().docTotal_Income(); // doctor is the class 
} 

毕竟这些我得到不规则的总和。我猜测我在某个地方出现了逻辑错误,但是我还有其他什么东西丢失了吗?

我很乐意欣赏这方面的任何建议。谢谢你的时间

+0

什么是“非正规求和”?你的总分是0.01吗?减去1,000,000?总数是[NaN](http://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#NaN)?无穷? – VGR

+0

不规则并不意味着大小或以往的任何错误。我试图解释它给出了错误的求和,5 + 5 = 20等等......我认为这是我的一个**逻辑错误**,但我无法指出它,所以把整个场景放入这个订单 – mustangDC

+0

把数字放到XxxTableModel中,覆盖模型定义里面的getColumnClass – mKorbel

回答

1
new doctor().docTotal_Income(); // doctor is the class 

首先,所有的类名应该以大写字母开头。 “医生()”应该是“Doctor()”。

你为什么要创建一个新的Doctor()?

如果您尝试过滤TableModel中的数据,那么您需要从表中获取数据,而不是TableModel。

所以,你的代码应该是这样的:

JTable table = easypath.doctorBusiness_table; 

for(int i=0; I < table.getRowCount(); i++) 
{ 
     sumTot += Double.parseDouble(table.getValueAt(i, totC).toString()); 
     sumTotx += Double.parseDouble(table.getValueAt(i, xC).toString()); 
     sumTotl += Double.parseDouble(table.getValueAt(i, lC).toString()); 
     sumTote += Double.parseDouble(table.getValueAt(i, eC).toString()); 
     sumTots += Double.parseDouble(table.getValueAt(i, sC).toString()); 
} 
+0

就像魅力一样,'getModel()'这个错误感谢了,你提到的其他东西,从长远来看一定会牢记在心。 *对于初学者来说学习是如此容易,像你这样的人@ camickr * – mustangDC

+0

还有一件事,我正在创建'新医生()....',因为这是一个不同的类'easypath'我打电话,我在做正确的事情还是应该改变一些事情? – mustangDC

+0

我不明白你的设计或理解你正在尝试做什么。但是,是的,我怀疑当你点击一个按钮时没有理由创建一个新班级。如果您要过滤表的结果,那么您让表过滤行,则无需创建新的TableModel。阅读关于“如何使用表格”的Swing教程以获得过滤示例。 – camickr