首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JComboBox监听器

JComboBox监听器
EN

Stack Overflow用户
提问于 2015-07-16 16:57:51
回答 1查看 38关注 0票数 0

我想根据JComboBox更改按钮的功能。

例如,当我选择单数和复数名词时,按钮的行为应该更改为此项,而当我从JComboBox按钮中选择另一项时,按钮的行为应该相应地更改。

EN

回答 1

Stack Overflow用户

发布于 2015-07-16 17:27:06

您可以将监听程序添加到组合框中:

代码语言:javascript
复制
//In your class
comboList.addActionListener(this);

然后,在ActionPerformed中,您可以更改所需功能的按钮的侦听器:

代码语言:javascript
复制
//In your class
JButton btn1 = new JButton("Button1");

--------------------------------------------------------

// Your actionPerformed for combo box listener
 public void actionPerformed(ActionEvent e) {
        JComboBox cb = (JComboBox)e.getSource();
        String itemName = (String)cb.getSelectedItem();
        if(itemName.equals("str"){

           //checking if you are not adding listener twice
           if(btn1.getActionListeners().length > 0){
                //remove all the existing listener, iterate and remove if more than one
                btn1.removeActionListener(existingListener);                    
           }
           btn1.addActionListener(new ButtonListener1());
        }
        if(itemName.equals("str2"){
           //follow same process as for above if
        }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31449725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档