Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3678722
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:22:32+00:00 2026-05-19T03:22:32+00:00

Does the method JComboBox.removeAllItems() in turn fire an ItemStateChanged event..? If so, how do

  • 0

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………..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T03:22:32+00:00Added an answer on May 19, 2026 at 3:22 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following method does not work because the inner block declares a variable of
The following method does not compile. Visual Studio warns An out parameter may not
What types and arguments does the method, Any when using Expression.Call take? I have
I thought I understood what the default method does to a hash... Give a
Like the title says Where and How (i.e. if encrypted, with what method) does
The method Concat() does not modify the original value. It returns a new value.
Why does the following method hang? public void pipe(Reader in, Writer out) { CharBuffer
Why does the Add method on the NameObjectCollectionBase insert at index position 0? Is
Take the method System.Windows.Forms.Control.Invoke(Delegate method) Why does this give a compile time error: string
I am designing a helper method that does lazy loading of certain objects for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.