简体   繁体   中英

Swing: how to get value from JTextfield in every JPanel?

My school project is to create a purchasing system for that I create a JPanel array to list out all the products information and also allow user to input something for every item. And I dont know how to get all the values from jtextfields by clicking one button. The actionPerformed() method always requires a final variable which is quite troublesome for me.

private JButton payBtn;

public void shoppingCartTab(Customer userIn){
    contentPanel.removeAll();
    bottomPanel.removeAll();
    final Customer USER = userIn;
    ArrayList<Product> pArray = new ArrayList<Product>();
    pArray = loadCartFile.loadCartFile(userIn);
    JLabel tabLabel  = new JLabel("Shopping Cart");
    JPanel cartItems = new JPanel();
    cartItems.setLayout(new BoxLayout(cartItems, BoxLayout.Y_AXIS));
    final JPanel CONSTANT_CART = cartItems;
    JScrollPane scroller = new JScrollPane(cartItems);  

    if(pArray != null){
        JPanel item[] = new JPanel[pArray.size()];
        for(int i = 0; i< pArray.size(); i++){

            item[i] = new JPanel(); 
            final JPanel JPANEL_TO_DEL = item[i];
            item[i].setLayout(new GridBagLayout());
            GridBagConstraints gBC = new GridBagConstraints();
            gBC.weightx = 0.3;
            item[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));

            JLabel icon_small = new JLabel(new ImageIcon("Icons\\" + pArray.get(i).getID() + "_small.jpg"));
            JLabel itemID = new JLabel(pArray.get(i).getID());  

            final String CONSTANT_ID = pArray.get(i).getID();
            JLabel itemName = new JLabel(pArray.get(i).getName());
            JLabel itemPrice = new JLabel("$" + pArray.get(i).getPrice());
            JPanel setQuantity = new JPanel();
            JButton plusBtn = new JButton("+");plusBtn.setPreferredSize(new Dimension(45,30));
            final JTextField QUANTITY = new JTextField("0");QUANTITY.setColumns(3);

            QUANTITY.setPreferredSize(new Dimension(45,30));QUANTITY.setHorizontalAlignment(JTextField.CENTER);
            JButton minusBtn = new JButton("-");minusBtn.setPreferredSize(new Dimension(45,30));

            plusBtn.addActionListener(new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent event){
                        if(Integer.parseInt(QUANTITY.getText())<100)
                            QUANTITY.setText(Integer.toString(Integer.parseInt(QUANTITY.getText())+1));
                    }
                });
            minusBtn.addActionListener(new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent event){
                        if(Integer.parseInt(QUANTITY.getText())>0)
                            QUANTITY.setText(Integer.toString(Integer.parseInt(QUANTITY.getText())-1));
                    }
                }); 

            setQuantity.add(plusBtn);
            setQuantity.add(QUANTITY);
            setQuantity.add(minusBtn);
            JButton delBtn = new JButton("Delete");
            delBtn.addActionListener(new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent event){
                        int dialogResult = JOptionPane.showConfirmDialog(null, "Are you sure to remove this item from your cart?", "Confirm", JOptionPane.YES_NO_OPTION);
                        if(dialogResult == JOptionPane.YES_OPTION){
                            CONSTANT_CART.remove(JPANEL_TO_DEL);
                            revalidate();
                            repaint();
                            ShoppingCart.removeItem(USER, CONSTANT_ID);
                        }
                    }
                }); 
            gBC.gridx = 0;
            gBC.gridy = 0;
            item[i].add(icon_small,gBC);
            gBC.gridx = 1;
            gBC.gridy = 0;
            item[i].add(itemID,gBC);
            gBC.gridx = 2;
            gBC.gridy = 0;
            item[i].add(itemName,gBC);
            gBC.gridx = 3;
            gBC.gridy = 0;
            item[i].add(itemPrice,gBC);
            gBC.gridx = 4;
            gBC.gridy = 0;
            item[i].add(setQuantity,gBC);
            gBC.gridx = 5;
            gBC.gridy = 0;
            item[i].add(delBtn,gBC);
            cartItems.add(item[i]);
        }

        contentPanel.add(tabLabel); 
        contentPanel.add(scroller);

        payBtn = new JButton("Pay");
        bottomPanel.add(payBtn); payBtn.addActionListener(this);
    }else{
        JLabel emptyMsg = new JLabel("Your cart is empty!");
        contentPanel.add(emptyMsg);
    }

    revalidate();
    repaint();
}

One solution would be to create a type which extends JPanel, and exposes a public property, which when called returns the value stored in the JTextField. Something like:

public class TextFieldPanel extends JPanel {

    private JTextField myTextField;

    public TextFieldPanel()
    {

        //layout init code here

    }

    public String getText()
    {
        return myTextField.getText();       
    }

}

Then just use this class instead of a regular JPanel. Then when your button is clicked, iterate over each of the objects in your collection, and call getText() on them to get your values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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