简体   繁体   English

JComboBox项目监听器

[英]JComboBox item listener

I have two combo boxes. 我有两个组合框。 The first contains some operator (+ , - ,* ,/) and the second one contains some value from 0 to 10. When user select (/) in first combo box I want the second one to show a value from 2 to 10 instead of 0 to 10. 第一个包含一些运算符(+,-,*,/),第二个包含0到10之间的值。当用户在第一个组合框中选择(/)时,我希望第二个显示2到10之间的值从0到10。

I've tried this: 我已经试过了:

String[] operators = {"+","-" ,"*", "/"};

String[] number = {"0","1","3"....."10"};

divisionModel= new DefaultComboBoxModel(new String[]{"2","3","4","5".."10"});



    operatorCombo = new JComboBox(operators);

    numberCombo = new JComboBox(number);


operatorCombo.addItemListener(new ItemListener() {
    public void itemStateChanged(ItemEvent e) {


    if (operatorCombo .getSelectedItem().equals("/")){

        numberCombo .setModel(divisionModel);
  }

my problem is when I select ("/") the numberCombo works fine and show me numbers from 2 to 10 but when I click on another operator it still show me the numbers from 2 to 10 instead 0 to 10.How can I solve this problem?! 我的问题是当我选择(“ /”)时,numberCombo可以正常工作并显示2到10的数字,但是当我单击另一个运算符时,它仍然显示2到10的数字而不是0到10的数字。问题?! Thanks 谢谢

// always compare objects using equals()
if (operatorCombo.getSelectedItem().equals("/")) {..

As to updating the 2nd combo, create a new model for it and call setModel(ComboBoxModel) . 至于更新第二个组合,请为其创建一个新模型,然后调用setModel(ComboBoxModel)

您可能会看到以下示例 ,该示例显示了在一个JComboBox所做的选择如何通过使用不同的DefaultComboBoxModel来更改相关JComboBox的外观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM