在 JComboBox 上添加 ListSelectionListener

发布于 2024-10-30 14:15:28 字数 420 浏览 2 评论 0原文

我对 Java Swing 还很陌生。我一直试图在 JComboBox 实例上添加 ListSelectionListener 。似乎只有 ListSelectionModel 接口有 addListSelectionListener 方法。我有点想不通......

为什么我想添加它是因为我希望程序做一些事情,即使组合框中的项目在选择后没有改变。

潜在答案 我只是想在组合框上附加一个动作监听器不起作用。我认为这是openjdk的错误。我已在此处报告了此问题,

提前致谢。

I'm quite new to Java Swing. And I'm stuck on trying to add a ListSelectionListener on a JComboBox instance. It seems only the ListSelectionModel interface has addListSelectionListener method. I kind of cannot figure it out...

Why I want to do add it is that I want program do something even the item in the combo box is not changes after selecting.

POTENTIAL ANSWER
I was simply thinking of attaching an actionListener on combobox not working. and i think it's bug of openjdk. I've reported it here

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

゛清羽墨安 2024-11-06 14:15:28

看一下 JComboBox#addItemListener

JComboBox combo = createCombo();
combo.addItemListener(new ItemListener()
{
    @Override
    public void itemStateChanged(ItemEvent e)
    {
        if (e.getStateChange() == ItemEvent.SELECTED)
        {
            Object selectedItem = e.getItem();
            // Do something with the selected item...
        }
    }
});

鼠标和键盘交互都会触发此事件。

Take a look at JComboBox#addItemListener:

JComboBox combo = createCombo();
combo.addItemListener(new ItemListener()
{
    @Override
    public void itemStateChanged(ItemEvent e)
    {
        if (e.getStateChange() == ItemEvent.SELECTED)
        {
            Object selectedItem = e.getItem();
            // Do something with the selected item...
        }
    }
});

This event is fired for both mouse and keyboard interaction.

流云如水 2024-11-06 14:15:28

对于 JComboBox,您必须使用 ActionListener。

    JComboBox jComboBox = new JComboBox();
    jComboBox.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("combobox event");

        }
    });

AFAIK,只要用户对 JComboBox 进行选择,即使它与已选择的项目相同,也会引发 actionPerformed。

For JComboBox, you'll have to use ActionListener.

    JComboBox jComboBox = new JComboBox();
    jComboBox.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("combobox event");

        }
    });

AFAIK, actionPerformed is raised whenever the user makes a selection for the JComboBox even if it's the same item that was already selected.

烟酉 2024-11-06 14:15:28

这取决于您的要求。 ActionEvent 仅在使用键盘时触发,而不是当鼠标在项目上移动时选择发生变化时触发。

如果您想在项目选择更改时执行某些操作,即使鼠标移动,那么您可能需要访问 JList。您可以使用以下代码访问弹出窗口使用的 JList:

JComboBox comboBox = new JComboBox(...);
BasicComboPopup popup = (BasicComboPopup)comboBox.getAccessibleContext().getAccessibleChild(0);
JList list = popup.getList();
list.addListSelectionListener(...);

It depends on your requirement. The ActionEvent is only fired when the keyboard is used, not when the selection changes as the mouse is moved over the items.

If you want to do some action when the item selection changes even if the mouse is moved then yes you will probably need access to the JList. You can access the JList used by the popup with the following code:

JComboBox comboBox = new JComboBox(...);
BasicComboPopup popup = (BasicComboPopup)comboBox.getAccessibleContext().getAccessibleChild(0);
JList list = popup.getList();
list.addListSelectionListener(...);
迟月 2024-11-06 14:15:28

使用 PopupMenuListener。当弹出菜单关闭时,获取所选索引并进行处理。

Use a PopupMenuListener. When the popup menu closes get the selected index and do your processing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文