Sort JComboBox Items

To sort items in a JComboBox object, check the code below. What the code does is compare the object to be added with the rest of the items that were already added. The compareToIgnoreCase() returns either a negative value if the first object less than the second one, 0 for equal and a positive value if it is greater than.

If the value returned is less than or equal to 0, the item will be inserted at the specified index of the item list else, add the item at the end of the item list.

Related Posts Plugin for WordPress, Blogger...


  1. hmm.. there must be something wrong with your netbeans. mine is ok. or probably netbeans has an option to have that manually set to on and off and you perhaps accidentally set it to off?

  2. Is this code complete and correct?

    It might be that it’s a bit too complicated for me to understand but I have to ask just the same.

    1. The latter part of the for-loop arguments looks strange (int i=0; i<= 0)

    2. The variables size and obj are not used

  3. This is funny. The for-loop looks totally wrong in the code box above but when I copy/paste the code in my comment and post it it comes out all right.

  4. Damn, we need a thread lock on our posts, you keep doing what I ask you seconds before my posts are published 🙂

    OK, I’m gonna give the new version a go. I will probably come back asking:

    – OK. it compiles. Now, how the hell do I use it? 🙂

  5. do you know how to use Java? 😉 you should study programming first.

    the combo is already the variable. do whatever you like.

    as for the sorting, no need to sort. that code overrode the addItem() method so it will sort the items automatically whenever you add new items to the JComboBo object

Leave a Reply

Your email address will not be published. Required fields are marked *