Java Swing Combobox removeAllItems调用ItemStateChanged也? [英] Java Swing Combobox removeAllItems calling ItemStateChanged also?

查看:448
本文介绍了Java Swing Combobox removeAllItems调用ItemStateChanged也?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码实际上非常简单。我看到一个简单而类似的代码来自这篇文章

My code is quite simple actually. I saw a simple and similar code was from this article.

首先,我有1个组合框。我有一个名为 itemStateChanged()的监听器。我加入这个监听器的目的是: 在用户点击(选择)其投放箱中的项目时执行某些代码

At first, I have 1 combobox. I have a listener on it called itemStateChanged(). My purpose to add into this listener is that; "to execute some code when user click (select) an item from its dropbox".

Cmb_ItemCategory = new javax.swing.JComboBox();

Cmb_ItemCategory.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Loading..." }));

Cmb_ItemCategory.addItemListener(new java.awt.event.ItemListener() {
    public void itemStateChanged(java.awt.event.ItemEvent evt) {
        Cmb_ItemCategoryItemStateChanged(evt);
    }
});

private void Cmb_ItemCategoryItemStateChanged(java.awt.event.ItemEvent evt) {

        if(evt.getStateChange() == java.awt.event.ItemEvent.SELECTED){
        System.err.println("Sombody click or change my model content");
        }

    }

在代码后面,我抓了一些数据,然后调用removeAllItems()的方法。
然后我将新模型(从新数据)设置到其中。

Behind the code, I grab some data, and then calling a method of removeAllItems() . And then I set a new Model (from new data) into it.

-- at another line of code ---
Cmb_ItemCategory.removeAllItems();
Cmb_ItemCategory.setModel(newModel);

当我这样做时,我意识到我的 itemStateChanged()被称为 strong> removeAllItem()方法。叫了一次。

I juz realized that my itemStateChanged() is called when i do the removeAllItem() method. called once.

那么,如何让它只调用一次用户单击(选择)而不是当removeAllItems()调用时?

So, How to make it only called once user Click (select) only AND not when removeAllItems() called?

它类似于这篇文章。但它不是removeItems案例。 CMIIW。

it similar to this article. But it's not removingItems case. CMIIW.

推荐答案

正如nIcE cOw在他的例子中已经说明的那样,当你使用时它肯定会有效DefaultComboBoxModel (在他的示例代码中就是这种情况,尽管它发生在屏幕后面)。

As nIcE cOw already illustrated in his example, it should certainly work when you use a DefaultComboBoxModel (which is the case in his sample code, although it happens behind the screens).

我可以解释你遇到的行为非 - DefaultComboBoxModel 案例,尽管您的代码段建议您使用一个。查看 JComboBox#removeAllItems 的源代码,有一个不同的代码路径,因为 removeAllElements 方法不属于 MutableComboBoxModel interface

I could explain the behavior you encounter for the non-DefaultComboBoxModel case, although your code snippet suggests you use one. Looking at the source code for JComboBox#removeAllItems there is a different code path since the removeAllElements method is not part of the MutableComboBoxModel interface

public void removeAllItems() {
    checkMutableComboBoxModel();
    MutableComboBoxModel<E> model = (MutableComboBoxModel<E>)dataModel;
    int size = model.getSize();

    if ( model instanceof DefaultComboBoxModel ) {
        ((DefaultComboBoxModel)model).removeAllElements();
    }
    else {
        for ( int i = 0; i < size; ++i ) {
            E element = model.getElementAt( 0 );
            model.removeElement( element );
        }
    }
    selectedItemReminder = null;
    if (isEditable()) {
        editor.setItem(null);
    }
}

因此非 - DefaultComboBoxModel 您将逐个删除项目。这意味着在某个特定时间点,您将删除所选元素。模型的可能实现可能会在此时更改所选元素。如果您在 DefaultComboBoxModel 中的实现中查找示例(虽然此代码不会被触发),您可以清楚地看到它更改了选择。

So with a non-DefaultComboBoxModel you are going to remove the items one by one. This means that at a certain point in time, you will remove the selected element. A possible implementation of your model might change the selected element at that point. If you look for example at the implementation in DefaultComboBoxModel (although this code will not be triggered) you can clearly see it changes the selection.

public void removeElementAt(int index) {
    if ( getElementAt( index ) == selectedObject ) {
        if ( index == 0 ) {
            setSelectedItem( getSize() == 1 ? null : getElementAt( index + 1 ) );
        }
        else {
            setSelectedItem( getElementAt( index - 1 ) );
        }
    }

    objects.removeElementAt(index);

    fireIntervalRemoved(this, index, index);
}

也许你的模型做了类似的事情,这解释了事件。只是为了使这篇文章完整, DefaultComboBoxModel #removeAllElements 背后的代码,你可以清楚地看到它将选择设置为 null 并且不选择其他对象。该代码中唯一奇怪的事情是它不首先触发 DESELECTED 事件,尽管如果你监听 intervalRemoved <你知道选择已经改变了/ code>事件......但这与您的问题无关?

Perhaps your model does something similar, which explains the event. Just for making this post complete, the code behind the DefaultComboBoxModel#removeAllElements where you can clearly see it sets the selection to null and does not select another object. Only weird thing in that code is that it does not fire a DESELECTED event first, although you know the selection has changed if you listen for the intervalRemoved event ... but that is not really relevant to your problem

public void removeAllElements() {
    if ( objects.size() > 0 ) {
        int firstIndex = 0;
        int lastIndex = objects.size() - 1;
        objects.removeAllElements();
        selectedObject = null;
        fireIntervalRemoved(this, firstIndex, lastIndex);
    } else {
        selectedObject = null;
    }
}

总结:我说问题的解决方案位于您的型号中,而不是您发布的代码

So to conclude: I say the solution for your problem is located in your model, and not in the code you posted

这篇关于Java Swing Combobox removeAllItems调用ItemStateChanged也?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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