在Java中获取行单元格中的总数

时间:2015-06-26 15:39:59

标签: java jtable

enter image description here

我在上面创建了一个演示。 JTable从MySQL数据库中检索“ID”,“名称”和“成本”。用户可以在“折扣”中输入数量(数量)和折扣价格。

当用户按下键盘上的TAB键移动到下一个单元格时,我想显示总计'总计'。

总计必须(费用*数量) - 折扣

我该如何实现?

我找到了一个解决方案:

String sql = "select ItemID,ItemName,CostPrice from druginfo where ItemID=?";
    try {   
        pst=conn.prepareStatement(sql);
        pst.setString(1, temp);

        rs=pst.executeQuery();
        addDataToTable(tableSale,DbUtils.resultSetToTableModel(rs));

        IDcombo.setSelectedItem(null);
        Namecombo.setSelectedItem(null);
        exptxt.setText(null);
        instock.setText(null);

        //getting user input for selling qty 
        String Qty=JOptionPane.showInputDialog("Insert Selling Quantity :");
        double sellingqty=Double.parseDouble(Qty);
        //setting qty input value to table sale  
        tableSale.getModel().setValueAt(sellingqty,i, 3);

        //getting user input for specific item discount
        String discount = JOptionPane.showInputDialog("Insert Item Discount");
        double idiscount=Double.parseDouble(discount);
        //setting input value to table sale  
        tableSale.getModel().setValueAt(idiscount,i, 4);

        //getting item cost from the table sale 
        double icost =(double) tableSale.getModel().getValueAt(i,2); 

        //calculating Gross Total  
        double grosstotal = (sellingqty*icost)-idiscount;

        //setting grosstotal value to table sale
        tableSale.getModel().setValueAt(grosstotal,i, 5); 

        String invoice = InvoiceNo_txt.getText(); 
        String id = (String) tableSale.getValueAt(i, 0);
        String name = (String) tableSale.getValueAt(i, 1);
        double dcost =  (double) tableSale.getValueAt(i, 2);
        String cost = String.valueOf(dcost);//converting double to string
        double dqty = (double) tableSale.getValueAt(i, 3);
        String qty = String.valueOf(dqty);//converting double to string
        double ditemDiscount = (double) tableSale.getValueAt(i, 4);
        String itemDiscount = String.valueOf(ditemDiscount);//converting double to string
        double dgrossTotal = (double) tableSale.getValueAt(i, 5);
        String grossTotal = String.valueOf(dgrossTotal);//converting double to string

2 个答案:

答案 0 :(得分:1)

查看here如何覆盖TableModelListener#tableChanged方法。如果找不到tab的解决方案,那么检查一下tableChanged事件处理程序执行相同的工作,在方法内部可能会在整个单元格中设置一个新值。

答案 1 :(得分:1)

您可以尝试查找TableModelListener ...将其添加到TableModel.In TableModelListener中检查更新单元格,然后检查已更新的列号。如果更新了折扣列,则获取需要计算的三列值将值设为总列。你可以参考here 尝试这样做非常简单,或者如果匆忙。请查看下面的代码

     table.getModel().addTableModelListener( new TableModelListener() {

        @Override
        public void tableChanged(TableModelEvent e) {
            if (e.getType() == TableModelEvent.UPDATE) {

                int row = e.getFirstRow();
                int column = e.getColumn();
                if (column == 4 ) {
                    TableModel model = table.getModel();

                   int cost =  (model.getValueAt(row, 2)==null)?0:((Integer)model.getValueAt(row, 2)).intValue();
                  int quantity =(model.getValueAt(row, 3)==null)?0: ((Integer) model.getValueAt(row, 3)).intValue();
                  int discount = (model.getValueAt(row, 4)==null)?0:((Integer) model.getValueAt(row, 4)).intValue();

                    Integer total = new Integer((quantity * price)-discount);
                    model.setValueAt(total, row, 5);
                }
            }
        }
    };