javaswingjcomboboxitemlistener

[JAVA]How to call the method "itemStateChanged"


So, I have this method;

public void itemStateChanged(ItemEvent event){
        if(event.getSource() == temasJogo){
            if(event.getStateChange() == ItemEvent.SELECTED){
                indiceTema = indiceTemas[ temasJogo.getSelectedIndex() ];
            }

        }
    }

just for this JComboBox

temasJogo = new JComboBox(temas);
        temasJogo.addActionListener(this);

I need it to modify an attribute of my class so that it selects the other theme of the game. The problem is, I can't manage to call this method anywhere. I know the answer will be something really simple, but I truly need help.


Solution

  • In theory, itemStateChanged is a method of ItemListener, assuming that you've implemented the interface in some way.

    In order to have it called, you need to register the instance of the ItemListener with the JComboBox

    temasJogo.addItemListener(this);
    

    as an example

    See How to Use Combo Boxes and How to Write an Item Listener for more details