JComboBox 选择更改侦听器? [英] JComboBox Selection Change Listener?

查看:39
本文介绍了JComboBox 选择更改侦听器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当从 JComboBox 中做出选择时,我都会尝试触发一个事件.

I'm trying to get an event to fire whenever a choice is made from a JComboBox.

我遇到的问题是没有明显的 addSelectionListener() 方法.

The problem I'm having is that there is no obvious addSelectionListener() method.

我尝试使用 actionPerformed(),但它从未触发.

I've tried to use actionPerformed(), but it never fires.

如果没有覆盖 JComboBox 的模型,我已经没有想法了.

Short of overriding the model for the JComboBox, I'm out of ideas.

如何在 JComboBox 上获得选择更改的通知?**

How do I get notified of a selection change on a JComboBox?**

我必须道歉.事实证明,我使用的是 JComboBox 的一个行为不端的子类,但由于您的回答很好,所以我将这个问题搁置一旁.

I have to apologize. It turns out I was using a misbehaving subclass of JComboBox, but I'll leave the question up since your answer is good.

推荐答案

它应该响应 ActionListeners,像这样:

It should respond to ActionListeners, like this:

combo.addActionListener (new ActionListener () {
    public void actionPerformed(ActionEvent e) {
        doSomething();
    }
});

@John Calsbeek 正确地指出 addItemListener() 也可以使用.但是,您可能会得到 2 个 ItemEvents,一个用于取消选择先前选定的项目,另一个用于选择新项目.只是不要同时使用这两种事件类型!

@John Calsbeek rightly points out that addItemListener() will work, too. You may get 2 ItemEvents, though, one for the deselection of the previously selected item, and another for the selection of the new item. Just don't use both event types!

这篇关于JComboBox 选择更改侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆