sasidhar
sasidhar

Reputation: 7752

How to identify the source of an event..?

Does the method JComboBox.removeAllItems() in turn fire an ItemStateChanged event..?
If so, how do i determine weather the event is generated due to the user action or due to the invocation of the method removeAllItems()..?
I have some code in the itemStateChanged() method of a combo box, which will work fine when the user changes the his selection in the combo box, but by the nature of code, it throws a null pointer exception when it is called due to the invocation of removeAllItems() on that combo box. If i can determine the cause for the event to be removeAllItems() method and return the control, then my problem would be solved. How do i do this..?
UPDATE
After you people have put up the answers, i used the following code...

 @Override
        public void itemStateChanged(ItemEvent e) {
            if(e.getSource()==branch_list)
            {
                if(!UserConstants.FID.contains("hod"))
                {
                    ignoreEvents=true;//static boolean variable not used by any other methods
                    year_list.removeAllItems();
                    year_list.addItem(" ");
                    sem_list.removeAllItems();
                    sem_list.addItem(" ");
                    branch_list.removeAllItems();
                    branch_list.addItem(" ");
                    if(branch_list.getSelectedItem().equals(" "))
                        return;
                    Iterator year_it=DatabaseConnector.map.get(branch_list.getSelectedItem().toString()).keySet().iterator();
                    while(year_it.hasNext())
                    {
                        year_list.addItem(year_it.next());

                    }
                    ignoreEvents=false;
                }
                return;
            }
            else if(e.getSource()==year_list)
            {
                if((branch_list.getSelectedItem()==null)||branch_list.getSelectedItem().toString().equals(" ")||ignoreEvents)
                return;
                sem_list.removeAllItems();
                section_list.removeAllItems();
                sem_list.addItem(" ");
                section_list.addItem(" ");
                Iterator i=DatabaseConnector.map.get(branch_list.getSelectedItem().toString()).get(year_list.getSelectedItem().toString()).iterator();
                int first=0;
                while(i.hasNext())
                {
                    String cur=i.next().toString();
                    if(first==0)
                    {
                        sem_list.addItem(cur.charAt(0));
                        section_list.addItem(cur.charAt(1));
                        first++;
                    }
                    else
                    {
                        section_list.addItem(cur.charAt(1));
                    }

                }


            }

        }
I am getting the following error:-  
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
    at sun.nio.cs.SingleByteEncoder.encodeArrayLoop(SingleByteEncoder.java:91)
    at sun.nio.cs.SingleByteEncoder.encodeLoop(SingleByteEncoder.java:130)
    at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:544)
    at sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:252)
    at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:106)
    at java.io.OutputStreamWriter.write(OutputStreamWriter.java:190)
    at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:111)
    at java.io.PrintStream.write(PrintStream.java:476)
    at java.io.PrintStream.print(PrintStream.java:619)
    at java.io.PrintStream.println(PrintStream.java:756)
    at attendence.Home.itemStateChanged(Home.java:427)

and so on...........

Upvotes: 2

Views: 1581

Answers (3)

aioobe
aioobe

Reputation: 421040

I believe your best option is to either

  1. Remove the listener when you're not interested in the events, even if it is temporary

    cb.removeItemListener(listener);
    cb.removeAllItems();
    cb.addItemListener(listener);
    

    or,

  2. Introduce a boolean variable called, say, ignoreEvents and do

    ignoreEvents = true;
    cb.removeAllItems();
    ignoreEvents = false;
    

    and let the handler method respect the ignoreEvents value.


Regarding your update:

You need to have

if (ignoreEvents == true)
    return;

at the top of your handler. Also, in your

if(branch_list.getSelectedItem().equals(" "))
    return;

you don't set ignoreEvents back to false. You could use a try / finally to make sure ignoreEvents is always set back to false when returning from the method.

Upvotes: 1

UVM
UVM

Reputation: 9914

This is the code for JComboBox.removeAllItem().

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

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

I think you need to add remove Item Listener.

Upvotes: 0

AlexR
AlexR

Reputation: 115338

did you try event.getSource() ?

Upvotes: 1

Related Questions