检查 JList 是否包含具有更改后缀的对象
Check if JList contains object with changing suffix
我需要检查 JList / DefaultListModel 是否包含项目。我正在检查的项目是一个字符串,它在“$”符号后发生变化。
这是我正在使用的代码的伪版本。
String theItem = "Bananas";
BigDecimal theQuantity = new BigDecimal(quantity.getText());
BigDecimal thePrice = new BigDecimal(0.00); //This changes depending on quanitity
thePrice = thePrice.setScale(2, BigDecimal.ROUND_HALF_UP);
if (!dlm.contains(whatGoesHere)) {
dlm.addElement(theItem + " $" + thePrice.toString());
jList.setModel(dlm);
//More code
} else {
JOptionPane.showMessageDialog(mainPanel, "You already selected that item", "Error Dialog", JOptionPane.ERROR_MESSAGE);
return;
}
我通过制作一个单独的 DefaultListModel 解决了这个问题,它只包含选定的项目。这用于验证 IF 语句。
这是工作代码:
DefaultListModel validatorDLM = new DefaultListModel(); //Specifically for validation
DefaultListModel orderDLM = new DefaultListModel();
String theItem = "Bananas"; //This changes with combo box
BigDecimal theQuantity = new BigDecimal(quantity.getText());
BigDecimal thePrice = new BigDecimal(0.00); //This changes depending on quanitity
thePrice = thePrice.setScale(2, BigDecimal.ROUND_HALF_UP);
if (!validatorDLM.contains(theItem)) {
validatorDLM.addElement(theItem);
orderDLM.addElement(theItem + " $" + thePrice.toString());
jList.setModel(orderDLM);
//More code
} else {
JOptionPane.showMessageDialog(mainPanel, "You already selected that item", "Error Dialog", JOptionPane.ERROR_MESSAGE);
return;
}
我需要检查 JList / DefaultListModel 是否包含项目。我正在检查的项目是一个字符串,它在“$”符号后发生变化。
这是我正在使用的代码的伪版本。
String theItem = "Bananas";
BigDecimal theQuantity = new BigDecimal(quantity.getText());
BigDecimal thePrice = new BigDecimal(0.00); //This changes depending on quanitity
thePrice = thePrice.setScale(2, BigDecimal.ROUND_HALF_UP);
if (!dlm.contains(whatGoesHere)) {
dlm.addElement(theItem + " $" + thePrice.toString());
jList.setModel(dlm);
//More code
} else {
JOptionPane.showMessageDialog(mainPanel, "You already selected that item", "Error Dialog", JOptionPane.ERROR_MESSAGE);
return;
}
我通过制作一个单独的 DefaultListModel 解决了这个问题,它只包含选定的项目。这用于验证 IF 语句。
这是工作代码:
DefaultListModel validatorDLM = new DefaultListModel(); //Specifically for validation
DefaultListModel orderDLM = new DefaultListModel();
String theItem = "Bananas"; //This changes with combo box
BigDecimal theQuantity = new BigDecimal(quantity.getText());
BigDecimal thePrice = new BigDecimal(0.00); //This changes depending on quanitity
thePrice = thePrice.setScale(2, BigDecimal.ROUND_HALF_UP);
if (!validatorDLM.contains(theItem)) {
validatorDLM.addElement(theItem);
orderDLM.addElement(theItem + " $" + thePrice.toString());
jList.setModel(orderDLM);
//More code
} else {
JOptionPane.showMessageDialog(mainPanel, "You already selected that item", "Error Dialog", JOptionPane.ERROR_MESSAGE);
return;
}