Swing: Is there a way to distinguish between a custom ItemEvent and an application invoked?

I work with combobox in a Swing-based application, and it’s hard for me to determine what to do to distinguish between the ItemEvent that is generated from the user event, and not because of the application.

For example, let's say I have a combobox, ` combo`, and I listen to itemStateChanged events with an ItemListener, ' listener'. When the user changes the selection to element 2 or I execute a line (pseudocode):

combo.setSelection(2)

.. it seems like I can’t tell about all these events.

However, I'm not a Swing expert in any way, so I thought I'd ask.

Thank!

+3
source share
5 answers

:). , . , "". , . , , . , GUI , . . - GUI. , . - , , , . EDT, . :

class ApplicationDataModel {

    private Flag current = Flag.RW;

    public void setData(ApplicationData data) {
        current = Flag.RO;
        setDataImpl(data);
        notifyObservers();
        current = Flag.RW;
    }

    public void reaction(Event e) {
        if (flag = Flag.RO) return;
        ...
    }

}

. setData , EDT . . ApplicationData ;). , .

+2

2 API setSelection (2), .

, , itemStateChanged . - ? , , .

. itemStateChanged Thread Dispatch Thread, , . , 100% .

+2

, ( , )...

, Java 6, ...

[]. , ( ) EDT SwingUtilities.invokeLater ( , )

0

, , , - . MVC , .

, , . .

0

, , , - , . , ( , ).

:

-, Swing. - , . , Swing .

. , "" , . JComboBox ComboBoxModel ComboBoxModel, . JComboBox ComboBoxModel . , :

User -- JComboBox -- ActionComboBoxModel -- DefaultComboBoxModel -- Application code
0

Source: https://habr.com/ru/post/1697947/


All Articles