繁体   English   中英

Java - 单击按钮时将项目从 arraylist 添加到 JTable

[英]Java - Adding items from arraylist to JTable when button clicked

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

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());
                            }
                        });
                    }

                });

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

首先,我建议您检查一下

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;
 }

暂无
暂无

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

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