【问题标题】:Java - Adding items from arraylist to JTable when button clickedJava - 单击按钮时将项目从数组列表添加到 JTable
【发布时间】:2026-02-10 21:30:01
【问题描述】:

我正在尝试为我的程序编写 GUI。我有一个 Product 类,我在其中将产品的价格和名称存储在 arraylist 中。我还有一个订单数组列表,其中包含给每个服务员的订单。我将所有产品放在一个 JComboBox 中,并为每个产品添加了一个动作侦听器,以通过更新 JLable 的文本在单击时显示每个产品的价格。然后有一个JSpinner来获取所选产品的数量。最后还有一个“添加”按钮,我想用它来更新 Jtable 中的产品名称、数量和总价,同时将该产品添加到订单数组列表中。我不知道如何填充 JTable,也无法从其他答案中了解多少,因为他们使用的是 netbeans。我想只使用一个简单的 JLabe,但在选择并添加每个产品后,我不明白如何更新文本并在标签中添加新行。你能解释一下我怎么能做到这一点吗?我的部分代码看起来像这样

box1.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        Product prod = (Product) box1.getSelectedItem();
        price.setText(String.valueOf(prod.getSellingPrice()));

        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int numbs = (Integer) spinner.getValue();
                for (int i = 0; i <= numbs; i++) {
                    order.addProduct(prod);
                }
                JLabel label = new JLabel();
                lists.add(label);
                label.setText(prod.getName() + " " + numbs + " " + numbs * prod.getSellingPrice());
            }
        });
    }
});

【问题讨论】:

    标签: java swing jtable


    【解决方案1】:

    如果我正确理解了您的问题,您希望在您的 gui 中有一个 JTable,如果单击按钮,它会显示订单(或可能的其他数据)。

    Firstable,我建议你检查一下

    https://docs.oracle.com/javase/tutorial/uiswing/components/table.html

    因为他们很好地解释了 JTable 的使用。

    无论如何,回答你的问题:

    首先,您必须创建一个表并将其添加到您的 gui:

    //creating a new JTable without any data and one column 'Orders'
    //you might wanna declare the JTable before that, since it will be referred to in the refresh() method
    JTable table = new JTable(new String[][]{{""}}, new String[]{"Orders"});
    //creating a scrollpane to put the table in, in case the table data exeeds the table
    JScrollPane scrollPane = new JScrollPane();
    //here you would e.g. set the bounds of the scrollpane etc
    scrollPane.setBounds(x,y,w,h)
    //setting the table in the scrollpane
    scrollPane.setViewportView(table);
    //adding the scrollpane to your contentPane
    contentPane.add(scrollPane);
    

    现在你想刷新表格,如果一个按钮被按下,所以我会在按钮的actionlistener中引用以下方法:

    //the method to refresh the table containing the orders (or possibly other data)
    void refresh(List<String> orders) {
        //creating a new TableModel for the table
        DefaultTableModel model = new DefaultTableModel();
        //set the data in the model to the data that was given, new Object[]{0} points to the 1st column
        model.setDataVector(getDataVector(data), new Object[]{"Orders});
        //set the model of the table to the model we just created
        table.setModel(model);
    }
    

    由于model.setDataVecor() 将行而不是列作为其第一个参数,因此您必须将数据拟合列表作为数据向量,例如使用以下方法:

    Object[][] getDataVector(List<String> data){
        Object[][] vector = new Object[data.size()][1];
        for(int i=0; i<data.size(); i++){
            vector[i][0] = data.get(i);
        }
        return vector;
     }
    

    【讨论】:

    • 你不应该调用fireTableDataChanged()。调用该方法是 TableModel 的责任。
    • 怎么样? @camickr
    • 您不应该从您的应用程序代码中调用任何 fireXXX(....) 方法。如果您调用模型上的方法,模型将调用适当的方法。如果您在表上使用 setModel(…) 方法,它将根据新模型重新创建 TableColumnModel。
    • @camickr 你说的完全正确,我适当地编辑了代码。