JTable错误的列求和值
JTable erroneous column summation values
场景 :
JTable 包含以下数据,我试图在下图中描述我正在尝试做的事情:-
所以,我想我可以在这里解释我想要实现的目标。
面临的问题
当然,不显示准确的结果(总和)。我使用的代码是:
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
}
在所有这些之后,我得到了不规则的求和。我猜我在逻辑上哪里出了问题,但是我还缺少其他东西吗?
如果有任何建议,我将不胜感激。感谢您的宝贵时间
new doctor().docTotal_Income(); // doctor is the class
首先 class 名称应以大写字符开头。 "doctor()" 应该是 "Doctor()".
你为什么要创建一个新的 Doctor()?
如果您尝试过滤 TableModel 中的数据,那么您需要从 table 而不是 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());
}
场景 :
JTable 包含以下数据,我试图在下图中描述我正在尝试做的事情:-
所以,我想我可以在这里解释我想要实现的目标。
面临的问题 当然,不显示准确的结果(总和)。我使用的代码是:
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
}
在所有这些之后,我得到了不规则的求和。我猜我在逻辑上哪里出了问题,但是我还缺少其他东西吗?
如果有任何建议,我将不胜感激。感谢您的宝贵时间
new doctor().docTotal_Income(); // doctor is the class
首先 class 名称应以大写字符开头。 "doctor()" 应该是 "Doctor()".
你为什么要创建一个新的 Doctor()?
如果您尝试过滤 TableModel 中的数据,那么您需要从 table 而不是 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());
}