帮助在 JComboBox 中的 ItemChange 时自动更改值
我有一个程序,其中使用了 3 个东西:一个复选框、一个组合框和一个文本字段。如果启用了复选框,则逻辑将如下所示,否则将启用组合框和文本字段,除非未启用。
然后通过将文本字段与组合框中的项目相乘来设置文本字段中的一些值。
从框架中 - 最终价格的值为价格 * 数量。
现在,当我点击购买时,问题一切顺利。但是,当我更改 Jcombobox 中的值时,它不会自动更改最终价格的值,并且仍为第一种情况下的 1200。对于要更改的值,我取消选中然后选中复选框。
可能是什么问题。我已将 ItemListner 用于复选框和组合框。
@Override
public void itemStateChanged(ItemEvent e){
Object get = e.getSource();
int multiplier;
int ftotal;
if (e.getStateChange()==ItemEvent.SELECTED){
if(get==chkbox1){
qntbox1.setEnabled(true);
size1.setEnabled(true);
multiplier = Integer.parseInt(String.valueOf(qntbox1.getSelectedItem()));
ftotal = Integer.parseInt(price1.getText()) * multiplier;
fprice1.setText(String.valueOf(ftotal));}
I have a program in which I am using 3 things, a checkbox, a combobox and a textfield. The logic works like this if checkbox is enable then combobox and textfield are enable unless not.
Then set some value in the textfield by mulitplying it with the item in combobox.
From the frame - The value of Final Price is Price * Quantity.
Now the issue when I click purchase everything went fine. But when I change the value from Jcombobox it doesn't automatically change the value in final price and remains to be 1200 as in first case. For the value to be changed I have uncheck and then check the Checkbox.
What could be the problem. I have used ItemListner for both checkbox and combobox.
@Override
public void itemStateChanged(ItemEvent e){
Object get = e.getSource();
int multiplier;
int ftotal;
if (e.getStateChange()==ItemEvent.SELECTED){
if(get==chkbox1){
qntbox1.setEnabled(true);
size1.setEnabled(true);
multiplier = Integer.parseInt(String.valueOf(qntbox1.getSelectedItem()));
ftotal = Integer.parseInt(price1.getText()) * multiplier;
fprice1.setText(String.valueOf(ftotal));}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须为您的
JComboBox
实现ActionListener
:You have to implement
ActionListener
for yourJComboBox
:不直接回答你的问题
1/ JCheckBox 完全是无用,最终计算确实需要它
2/考虑 JComponents 的
Price
和Final Price
将仅为 JFormattedTextField,那么你很可能会忘记Parse#Whatever
3/ 考虑一下
Quantity
的JComponents
将仅为 JSpinner,但是 Number 实例的解决方法会比JFormattedTextField
示例稍微复杂此处4/为了获得良好的输出,请将所有内容放入 JTable
5/对于 JComboBox 我更喜欢 ItemListener 不是 ActionListener,因为您的问题不在于正确的
Listener
但以正确的方式解析Numbers
not directly to your question
1/ JCheckBox is totally useless, that will be really needed for final calculation(s)
2/ consider that JComponents for
Price
andFinal Price
would be only JFormattedTextField, then you can pretty to forgot forParse#Whatever
3/ consider that
JComponents
forQuantity
would be only JSpinner, but workaround for Number Instance would be litte bit complicated as forJFormattedTextField
example here4/ for nice output put everything to the JTable
5/ for JComboBox I preferred ItemListener not ActionListener, because your problems isn't with proper
Listener
but with parsingNumbers
correct way好的,它工作了。 ActionListner 使其工作(JComboBox)。我猜想对太多组件使用 ItemListner 会使解析有点混乱,此外我在 ItemListner 范围中使用了太多子句。非常感谢大家的帮助。
@mKorbel:我将尽快使用您的建议:)并将检查 JTable 和所述组件。因为我还没有使用过它们,所以必须经过它们。
@Eng.Fouad:谢谢大家的帮助。
只有一个问题。当我将 getSelectedItem() 类型转换为整数时,它会给出 NumberFormatException 错误(运行时)。所以我必须先将对象更改为字符串,然后将其解析为整数。任何线索为什么直接转换会抛出错误?
这是该项目的工作代码。
Ok got it working. The ActionListner made it work (JComboBox). I guess using ItemListner for too many components made parsing a little confusing, add to that I used too many clauses in the ItemListner scope. Thanks a lot everyone for helping.
@mKorbel : I'll be using your suggestion asap :) and will check JTable and said components. have to go through them since I haven't used it.
@Eng.Fouad : Thanks man for the help.
Just one issue. When I typecast getSelectedItem() to integer it gives NumberFormatException error (runtime). So I have to first change the object to String and then parseit into integer. Any clue why direct conversion is throwing error ?
Here is the working code for the project.