在Java中总结Jtable coumn值

时间:2017-12-27 09:14:57

标签: java swing jtable textfield

伙计们,我的问题是关于jtable列值,我遇到了问题。我想将jtable列值相加并将其放入文本字​​段中。我正在将值插入jtable中,如果一行为空,则不会计算总数。我无法理解这是什么问题。我创建了一个方法并在按钮事件上调用它。

public void getSum(){

    int total=0;
    for(int i=0; i<JV_tbl.getRowCount(); i++){
        int amount = Integer.parseInt((String) JV_tbl.getValueAt(i, 6));

        total+=amount;
    }
    JV_totalDebit_box.setText(String.valueOf(total));

}

和按钮事件代码。

private void btn_entrySaveActionPerformed(java.awt.event.ActionEvent             evt) {                                              
     model = (DefaultTableModel)JV_tbl.getModel();
     model.addRow(new Object[]{
         jv_no_box.getText(),
         JV_entry_box.getText(),
         ((JTextField)date_txt.getDateEditor().getUiComponent()).getText(),
         JV_Acc_code.getText(),
         JV_acc_title.getText(),
         JV_desc_box.getText(),
         JV_debit_box.getText(),
         JV_credit_box.getText(),
         prep_box.getText(),
         checked_box.getText(),
         approved_box.getText()

     });

     getSum();

}               

1 个答案:

答案 0 :(得分:0)

更改你的getSum()函数,如下所示。它将删除所有空白行,然后继续完成。

require(magrittr)
require(officer)

TARGET_FILE = "ph_add_fpar.pptx"
setwd(file.path("E:", "Work", "Reporting"))
file.remove(TARGET_FILE)

MASTER_LAYOUT = "Blank"
PICTURE_AND_TEXT_PAGE_LAYOUT = "Internal Slide - Picture and Text"

bold_face <- shortcuts$fp_bold(font.size = 30)
bold_redface <- update(bold_face, color = "red")

fpar_1 <- fpar(ftext("Hello ", prop = bold_face),
              ftext("World", prop = bold_redface ),
              ftext(", \r\nhow are you?", prop = bold_face ) )

fpar_2 <- fpar(ftext("Hello ", prop = bold_face),
               ftext("World", prop = bold_redface ),
               ftext(", \r\nhow are you again?", prop = bold_face ) )

doc <- read_pptx(path = file.path(getwd(), "Template.pptx")) %>%
  add_slide(layout = PICTURE_AND_TEXT_PAGE_LAYOUT, master = MASTER_LAYOUT) %>%
  ph_empty_at(left = 1,top = 1,width = 3,height = 2,template_type = "body",template_index = 4) %>%
  ph_empty_at(left = 4,top = 1,width = 3,height = 2,template_type = "body",template_index = 4) %>%
  ph_add_fpar(value = fpar_1, type = "body", level = 1, id_chr = "2") %>%
  ph_add_fpar(value = fpar_2, type = "body", level = 1, id_chr = "3")

print(doc, target = TARGET_FILE)

system("cmd.exe", input = TARGET_FILE)