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

  • Home
  • SEARCH
  • 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 3239862
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:01:10+00:00 2026-05-17T18:01:10+00:00

It seems no selected or deselected ItemEvent s are generated for the null-item in

  • 0

It seems no selected or deselected ItemEvents are generated for the null-item in the JComboBox. How can I change this? Making the item "" is not an option.

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;

public class ComboBoxTest {
   public static void main(String... args) {
       JComboBox cb = new JComboBox(new String[]{null, "one","two","three"});
       cb.addItemListener(new ItemListener(){
            public void itemStateChanged(ItemEvent e) {
                System.out.println(e);
            }
       });
       JOptionPane.showMessageDialog(null, cb);
  }
}
  • 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-17T18:01:10+00:00Added an answer on May 17, 2026 at 6:01 pm

    Null objects will not play nicely in a JComboBox. For example, the combo box’s getSelectedIndex method, which is fired when you select an item, will return -1 if the object is null. There may also be other methods which perform null checks and may return incorrect results.

    If you really need this functionality, it would be better to use wrapper objects. For example:

    class StringWrapper{
        final String s;
        public StringWrapper(String s){
            this.s=s;
        }
        @Override
        public String toString() {
            return s;
        }
    }
    
    final JComboBox cb = new JComboBox(new StringWrapper[]{ 
                new StringWrapper(null), 
                new StringWrapper("one"),
                new StringWrapper("two"),
                new StringWrapper("three")});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems that ComboBoxes loose their selected item after their dataProvider updates, even if
Seems so basic, I can't believe I don't know this! I just need a
This seems pretty basic, but I can't find the best method to do this...
This seems like it should be something fairly simple, but I just can't seem
This seems simple enough, but I'm not able to figure out why this code
In struts, it seems there is no selected option. The HTML option tag has
seems like I'm stuck with jQuery tabs. I'm trying to pass selected tab name
Seems to me most of developers completely ignore this features. People prefer handling security
Seems like a simple enough question but I can't seem to find the answer.
Seems like as really simple thing to do, but I just can't track it

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.