简体   繁体   中英

How can I add element into a JList? inventory products program

I'm creating an inventory program which will allow user to add,modify, sell or remove product. I created a class Product, a class for the GUI and one for the ArrayList. I'm having problems adding the products to the JList and to the ArrayList, I want that once the user will press the button (addButton1) the product will be added either in the JList and in the ArrayList. could anyone help me? It will be really appreciated, thanks in advance.

This is my Product class

//attributes
{
    private String name;
    private int stockLevel;
    private double price;

//methods
public Product(String nameIn, int stockLevelIn, double priceIn) 
{
    name = nameIn;
    stockLevel = stockLevelIn;
    price = priceIn;
}
//all the methods here}

This is the GUI class

import javax.swing.*;
import java.awt.*;
public class ProductGUI extends JFrame implements ActionListener
{               
        private JFrame AddJFrame = new JFrame();
        private JTextField productNameText = new JTextField(15);
        private JTextField productPriceText = new JTextField(15);
        private JTextField productStockLevelText = new JTextField(15);
        private JButton addButton1 = new JButton("Add");
        private JButton cancelButton = new JButton("Cancel");

                //menu
        private JMenuBar bar = new JMenuBar();
        private JMenu productMenu = new JMenu("Product");
        private JMenu infoMenu = new JMenu("Info");
        private JMenuItem addProductItem = new JMenuItem("Add");
    private JMenuItem modifyProductItem = new JMenuItem("Modify");
    private JMenuItem removeProductItem = new JMenuItem("Remove");
        private JMenuItem sellProductItem = new JMenuItem("Sell");
        private JMenuItem aboutApp = new JMenuItem("About App");

                //Buttons
        private JButton sellButton = new JButton("Sell");
        private JButton addButton = new JButton("Add");
        private JButton modifyButton = new JButton("Modify");
        private JButton removeButton = new JButton("Remove");

                //Labels
        private JLabel productName = new JLabel("Product Name: ");
        private JLabel productPrice = new JLabel("Price: ");
        private JLabel productStockLevel = new JLabel("Stock level: ");

        //panels
    private JPanel topPanel = new JPanel();
    private JPanel bottomPanel = new JPanel();
    private JPanel middlePanel = new JPanel();

                //List
        private JScrollPane scrollPane;
        private JList<String> listArray;
        private DefaultListModel modelList = new DefaultListModel();

        //first object
    private ProductCollection collection;


        //constructor
    public ProductGUI() {
        setTitle("Product Inventory");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 500);


            //AddJFrame
        AddJFrame.setTitle("Add Product");
        AddJFrame.setSize(300,300);
        AddJFrame.setLayout(null);
        AddJFrame.add(addButton1);
        addButton1.setBounds(200, 200, 70, 30);
        addButton1.addActionListener(this);
        AddJFrame.add(productNameText);
        AddJFrame.add(productStockLevelText);
        AddJFrame.add(productPriceText);
        AddJFrame.add(cancelButton);
        cancelButton.setBounds(70, 200, 75, 30);
        AddJFrame.add(productName);
        productName.setBounds(40, 13, 90, 15);
        productNameText.setBounds(140, 10, 90, 20);
        AddJFrame.add(productStockLevel);
        productStockLevel.setBounds(40, 30, 100, 100);
        productStockLevelText.setBounds(140, 70, 90, 20);
        AddJFrame.add(productPrice);
        productPrice.setBounds(40, 95, 100, 100);
        productPriceText.setBounds(140, 135, 90, 20);


        //Menu
        setJMenuBar(bar);
        bar.add(productMenu);
        productMenu.add(addProductItem);
        productMenu.add(modifyProductItem);
        productMenu.add(sellProductItem);
        productMenu.add(removeProductItem);
        addProductItem.addActionListener(this);
        modifyProductItem.addActionListener(this);
        sellProductItem.addActionListener(this);
        removeProductItem.addActionListener(this);
        bar.add(infoMenu);
        infoMenu.add(aboutApp);

        //Panel Layout
        add(BorderLayout.NORTH, topPanel);
        topPanel.setBackground(Color.white);
        topPanel.setPreferredSize(new Dimension(250, 250));
        topPanel.setLayout(null);
        middlePanel.setLayout(null);


        //TopPanel buttons
        topPanel.add(addButton);
        removeButton.setBounds(350, 190, 80, 30);
        topPanel.add(modifyButton);
        sellButton.setBounds(250, 190, 80, 30);
        topPanel.add(removeButton);
        addButton.setBounds(50, 190, 80, 30);
        topPanel.add(sellButton);
        modifyButton.setBounds(150, 190, 80, 30);
        addButton.addActionListener(this);

        //collection object
        collection = new ProductCollection();
            //topPanel list
            scrollPane = new JScrollPane();
                topPanel.add(scrollPane);
        scrollPane.setBounds(50, 10, 380, 150);
                modelList = new DefaultListModel();
                JList listArray = new JList(modelList);
                scrollPane.setViewportView(listArray);
                modelList.addElement("gfrew");


        //set frame visible

        setVisible(true);

}


        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource()==addButton || e.getSource()==addProductItem)
            {
                AddJFrame.setVisible(true);
            }
            if(e.getSource()==addButton1)
            {
                if (productNameText.getText().equals("") && productStockLevelText.getText().equals("") && productPriceText.getText().equals(""))
                {
                    try{
                        String productStockString = productStockLevelText.getText();
                        int productStockInt = Integer.parseInt(productStockString);
                        String productPriceString = productPriceText.getText();
                        double productPriceDouble = Double.parseDouble(productPriceString);
                        Product objectX = new Product(productNameText.getText(), productStockInt,productPriceDouble);
            collection.addProduct(objectX);
            modelList.addElement(objectX.getName());
                        }

                        catch (NumberFormatException f) {JOptionPane.showMessageDialog(getContentPane(),"Couldn't add the product, you didn't enter a number in the Stock/Price field", "Error",JOptionPane.ERROR_MESSAGE);
                        }
                    } 

                }}

This is the ArrayList class

import java.util.*;
public class ProductCollection {
    private ArrayList<Product> productsArrayList;

public ProductCollection(){
productsArrayList = new ArrayList<Product>();
}
public void addProduct(Product newProduct) {
        productsArrayList.add(newProduct);
    }
public void removeProduct(int removeProduct) {
        productsArrayList.remove(removeProduct);
    }
public ArrayList<Product> getProducts() {
        return productsArrayList;
    }

}

在此输入图像描述 Well I ran your application, and removing the

if (productNameText.getText().equals("") && productStockLevelText.getText().equals("") && productPriceText.getText().equals(""))
                {

fixed it. Are you trying to make sure the fields aren't empty? If so it would be

if (!productNameText.getText().equals("") && !productStockLevelText.getText().equals("") && !productPriceText.getText().equals(""))
                {

Besides for that, your class setup, arraylist, model list, and product class are all fine and work. In your arraylist class, do not forget to add a getSize() method and keep track of how much it is getting. This will be important for looping later.

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