How can I dynamically change the number of items in a JComboBox

private void dropDownMenu(JPanel jp1, String prodId){
    int len = storeManager.getInv().getStockAmount(prodId);
    int[] nums = new int[len];
    String[] numPossible = new String[len];

    for (int i=0; i<len; i++){
        nums[i] = i+1;
    }
    for (int i=0; i<len; i++){
        numPossible[i] = String.valueOf(nums[i]);
    }
    JComboBox<String> cb = new JComboBox<String>(numPossible);
    JButton okButton = new JButton("Add To Cart");
    okButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Product p1 = storeManager.getInv().getProd(prodId);
            String quan = (String) cb.getSelectedItem();
            int quantity = Integer.parseInt(quan);
            if (quantity > storeManager.getInv().getStockAmount(prodId)) {
                System.out.println("Not Enough Stock.");
            } else {
                storeManager.getCart().addToCart(p1, quantity);
                storeManager.getInv().removeStockAmount(prodId, quantity);
                //update the dropdown menu here
            }
        }
    });
    jp1.add(cb);
    jp1.add(okButton);
}

Essentially what i am looking for is that whenever i select a number from the drop down menu, i want the number of items in the menu to be reduced by the amount that was added to cart. for example if i add 5 to cart then i want the dropdown menu to go from allowing me to choose 10 to 5 only. Image of GUI

Answer

As a thought…Instead of doing all these conversions from integer to string and string to back to integer in order to fill your combo box, why not just have a combo box of Integer? You’re dealing initially with integer quantity values anyways:

JComboBox<Integer> cb = new JComboBox<>();
int len = storeManager.getInv().getStockAmount(prodId);
for (int i = 1; i <= len; i++) {
   cb.addItem(i);
}
cb.setSelectedIndex(0); 

Your action listener might look something like this now:

okButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        Product p1 = storeManager.getInv().getProd(prodId);
        int quantity = (int) cb.getSelectedItem();
        /* This 'if' statement below would be moot if the Combo-Box 
           is properly updated unless editing is allowed in the combo
           which in this case...disable that feature.     */
        if (quantity > storeManager.getInv().getStockAmount(prodId)) {
            System.out.println("Not Enough Stock.");
        } else {
            storeManager.getCart().addToCart(p1, quantity);
            len = storeManager.getInv().removeStockAmount(prodId, quantity);
            cb.removeAllItems();
            for (int i = 1; i <= len; i++) { cb.addItem(i); }
            cb.setSelectedIndex(0); 
        }
    }
});

Possibly better yet would be to utilize the JSpinner component instead of a Combo Box. A drop-down list in this use case always seems a bit obtrusive in my opinion.

Leave a Reply

Your email address will not be published. Required fields are marked *