Java 单击JButton后更新JTable

Java 单击JButton后更新JTable,java,swing,jtable,jframe,rows,Java,Swing,Jtable,Jframe,Rows,当我点击“销售”按钮时,表格必须在第一行显示我销售的商品以及价格。但当我再次单击时,它会删除最后一行,并创建一个新表。如何使用新数据创建新行? 这是我的代码: if (e.getSource()==sale) { int tempcod=0,tempcant=0; //tempcod is the code of the product, temocant is the number of products sold final DefaultTa

当我点击“销售”按钮时,表格必须在第一行显示我销售的商品以及价格。但当我再次单击时,它会删除最后一行,并创建一个新表。如何使用新数据创建新行? 这是我的代码:

if (e.getSource()==sale)
    {
        int tempcod=0,tempcant=0; //tempcod is the code of the product,
       temocant is the number of products sold

    final DefaultTableModel model = new DefaultTableModel();

        model.addColumn("Product");
        model.addColumn("Price");

        try
        {
        tempcod=Integer.parseInt(cod.getText());
        tempcant=Integer.parseInt(quantity.getText());
        }
        catch(NumberFormatException a)
        {
            JOptionPane.showMessageDialog(null, "Invalid Number");
        }

        for (int j=0;j<nulo; j++) //nulo is the number of items at sale
        {

            if (tempcod==codigo[j]) //id the code i wrote exist in 
                                      the list of products
            {
                for (int k=0; k<nulo; k++)
                {
                    if (tempcod==ventaActual[0][k])
                    {
                        ventaActual[1][k]+=tempcant;
                    }
                    else
                    {
                    ventaActual[0][k]=tempcod;
                    ventaActual[1][k]=tempcant;
                    }
                }

                model.insertRow(model.getRowCount(), new Object[]{item[j],price[j]*tempcant});//this line creates the row of the table.
          //item[] is the name of the item, price[] is the price of the item
                JTable Lista= new JTable(model);
                lista1=new JScrollPane(Lista);
                lista1.setBounds(170,150,200,200);
                lista1.setEnabled(false);
                lista1.setVisible(true);
                this.add(lista1);

                a+=(tempcant*precio[j]);
                String b=Integer.toString(a);
                tot.setText(b);
                ventaInforme=ventaActual;
            }
            else
            {
                cod.setText(null);
                cantidad_n.setText(null);
            }
        }
    }
if(e.getSource()==sale)
{
int tempcod=0,tempcont=0;//tempcod是产品的代码,
temocant是销售的产品数量
最终DefaultTableModel=新DefaultTableModel();
型号。添加列(“产品”);
型号。添加栏(“价格”);
尝试
{
tempcod=Integer.parseInt(cod.getText());
tempcont=Integer.parseInt(quantity.getText());
}
捕获(数字格式异常a)
{
showMessageDialog(null,“无效数字”);
}

对于(int j=0;j您对model.insertRow的调用应该在表中插入一行,并触发必要的事件以更新表本身。但是,您需要从现有表中获取现有模型,然后更新该模型

e、 g.DefaultTableModel=(DefaultTableModel)myTable.getModel()


您可能还应该删除创建新表并将其添加到容器中的代码,除非这是您想要的。

您对model.insertRow的调用应该在表中插入一行,并触发必要的事件以更新表本身。但是,您需要从现有表中获取现有模型,然后更新在模型上

e、 g.DefaultTableModel=(DefaultTableModel)myTable.getModel()

您可能还应该删除创建新表并将其添加到容器中的代码,除非这是您想要的