简体   繁体   English

在Jtable列上添加值

[英]Adding value on a Jtable column

I want to add how many items in the unit count and compute it in the total cost. 我想在单位计数中添加多少个项目并在总成本中进行计算。

在此处输入图片说明

private void display()
{
    DefaultTableModel model= (DefaultTableModel)SupplyRSTable.getModel();
        SupplyRSTable.revalidate();
        model.getDataVector().removeAllElements();
        try{

         stmt = conn.createStatement();
     stmt.executeQuery("SELECT itemcode,itemname,itemgroup,itemcost FROM supplytable");
     rs = stmt.getResultSet();
        while (rs.next()) 
        {
                    String r1 = rs.getString("itemname");
                    String r2 = rs.getString("itemgroup");
                    String r3 = rs.getString("itemcost");
                    String r4 = rs.getString("itemcode");
                   model.addRow(new Object[] {r4,r1,r2,r3}); 
                   SupplyRSTable.revalidate(); 

        }
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }`

I only have a display 我只有一个显示器

1: you formated your code wrong; 1:您的代码格式错误; only part of it is formatted in this post. 本文仅格式化其中的一部分。

2: I'm assuming your database that your are pulling from has a column which has the unit count so the query should be something like : 2:我假设您要从中提取的数据库具有一列,该列具有单位计数,因此查询应类似于:

stmt.executeQuery("SELECT itemcode,itemname,itemgroup,itemcost,itemcount FROM supplytable");

The idea is to create that total column using previous value of item cost and item count that is already there so it should look like this: 这个想法是使用之前已经存在的物料成本和物料计数值创建该总计列,因此应如下所示:

String r1 = rs.getString("itemname");
String r2 = rs.getString("itemgroup");
String r3 = rs.getString("itemcost");
String r4 = rs.getString("itemcode");
String r5 = rs.getString("itemcount");
Integer cost= Integer.parseInt(r5);
Integer amount=Integer.parseInt(r3);
Integer total=cost*amount;
String r6=Integer.toString(total);
model.addRow(new Object[] {r4,r1,r2,r3,r5,r6});
SupplyRSTable.revalidate(); 

of course Multiplying an Integer Cost by an Amount could give rise to decimal value such as 31.43$ so you can/should replace the total variable with a Double and you would need to change r6 Integer parsing aswell in this case. 当然,将整数成本乘以金额可能会导致十进制值(例如31.43 $),因此您可以/应该将总变量替换为Double,并且在这种情况下还需要更改r6整数解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM