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 8842335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:55:54+00:00 2026-06-14T10:55:54+00:00

I have 2 JComboBox controls filled with items initially. Selecting one item in a

  • 0

I have 2 JComboBox controls filled with items initially. Selecting one item in a JComboBox moves it to the other JComboBox.
The problem occurs when one of the JComboBox controls is empty. When I try to move an item to an empty JComboBox it triggers its actionPerformed event.

private void leftComboActionPerformed(java.awt.event.ActionEvent evt) {                                          
        Object selectedItem = leftCombo.getSelectedItem();
        leftCombo.removeItem(selectedItem);
        rightCombo.addItem(selectedItem);
    }                                         

    private void rightComboActionPerformed(java.awt.event.ActionEvent evt) {                                           
        Object selectedItem = rightCombo.getSelectedItem();
        rightCombo.removeItem(selectedItem);
        leftCombo.addItem(selectedItem);
    }

Why does adding an item to the empty JComboBox do that? It doesn’t fire when an item is added to it when it’s filled.
How to prevent the event from firing when it’s empty?

  • 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-06-14T10:55:55+00:00Added an answer on June 14, 2026 at 10:55 am

    The following piece of code dumps the stack trace

    import javax.swing.JComboBox;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class EmptyComboboxTest {
      public static void main( String[] args ) {
        EventQueue.invokeLater( new Runnable() {
          @Override
          public void run() {
            JComboBox<String> emptyBox = new JComboBox<String>();
            emptyBox.addActionListener( new ActionListener() {
              @Override
              public void actionPerformed( ActionEvent e ) {
                Thread.dumpStack();
              }
            } );
            emptyBox.addItem( "test" );
          }
        } );
      }
    }
    

    The relevant part of it

    at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1260)
    at javax.swing.JComboBox.contentsChanged(JComboBox.java:1331)
    at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:118)
    at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:94)
    at javax.swing.DefaultComboBoxModel.addElement(DefaultComboBoxModel.java:132)
    

    If you then look in the source code, you can see that when adding an element it will be selected when there is currently nothing selected

        if ( objects.size() == 1 && selectedObject == null && anObject != null ) {
            setSelectedItem( anObject );
        }
    

    Changing the selection might cause the firing of an ActionEvent. If you follow the source code there is no mechanism to disable this. You could however disable your listener before you add the element, and enable it afterwards.

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

Sidebar

Related Questions

The problem is strange, I have a combobox which is correctly filled with items
I do have a strange focus problem when using a JComboBox inside an embedded
I have a little problem. I had some JComboBox to a JDialog but they
I'm trying to fill a jComboBox with objects. I have it working in one
I have 10 ComboBox controls that will use the same item template (an Image
I have a problem with controls nested in a TabControl. I have a TabControl
I have a wpf cusotm control derived from combobox System.Windows.Controls.ComboBox the control template is
I have a Form with multiple different controls like ComboBox , TextBox and CheckBox
I have a JTable with two columns and both are JComboBox, for this purpose
I have two combobox controls. If i change combobox A then I am reading

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.