[英]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.基本上我正在寻找的是,每当我 select 下拉菜单中的一个数字时,我希望菜单中的项目数量减少添加到购物车的数量。 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.
例如,如果我将 5 添加到购物车,那么我希望 go 的下拉菜单只允许我选择 10 到 5。 Image of GUI
图形用户界面图像
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?作为一个想法......而不是进行所有这些从 integer 到字符串和字符串到 integer 的转换以填充您的组合框,为什么不只是有一个 ZA0FAEF0851B4294C4406F2B94BBCB1 的组合框? You're dealing initially with integer quantity values anyways:
无论如何,您最初都是在处理 integer 数量值:
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 theJSpinner component instead of a Combo Box.可能更好的是使用JSpinner组件而不是组合框。 A drop-down list in this use case always seems a bit obtrusive in my opinion.
在我看来,这个用例中的下拉列表总是显得有点突兀。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.