Java Tips Weblog

  • Blog Stats

    • 1,760,809 hits
  • Categories

  • Archives

Sorted Combo Box Model

Posted by Rob Camick on November 11, 2008

A JComboBox displays the items in the drop down list in the order in which the items where added to the combo box. If you want the items sorted then you can use the Collections.sort() or Arrays.sort() methods on the data before you create the combo box model. But what if you have a dynamic combo box and you want to keep the items in sorted order as new items are added?

Well, then you can use the SortedComboBoxModel. This model extends the DefaultComboBoxModel and has two additional pieces of functionality built into it:

  • upon creation of the model, the supplied data will be sorted before the data is added to the model
  • when adding new items to the model, the items will be inserted so as to maintain the sort order

The default sort order will be the natural sort order of the items added to the model. However, you can control this by specifying a custom Comparator as a parameter of the constructor.

When you create the combo box with code like the following…

String[] items = { "one", "two", "three" };
SortedComboBoxModel<String> model = new SortedComboBoxModel<String>(items);
JComboBox<String> comboBox = new JComboBox<String>( model );

… then the combo box will look like this when first displayed:


Get The Code

11 Responses to “Sorted Combo Box Model”

  1. Mouna said

    Very useful, thanks for sharing this!

  2. Sandwich said

    Are there any restrictions on the circulation of this code? Do you mind if I use it in a commercial project?

    • Rob Camick said

      As I stated on the About page:

      You are free to use and/or modify any or all code posted on the Java Tips Weblog without restriction. A credit in the code comments would be nice, but not in any way mandatory.

      Good luck with your project.

  3. Zen said

    Im wondering… I would love to use this combobox. The problem is though, all the items in the combobox has to be taken from a txt-file. The reason being that someone else, at a later stage, should be able to change the items without entering the program. How do I do that?

    At the moment this is what i have…
    one java-file with stringreader, where I have a problem naming the rows.
    one GUI-file with combobox + others.

    Since I´m pretty new at this i need all help I can get…

    • Rob Camick said

      I only answer question about the functionality of this class here. Your question about loading data from a file to a combo box should be asked in a forum. I suggest you look at the DefaultComboBoxModel API. You can create the model with an Array a Vector or add individual elements to the model. This class extends the DefaultComboBoxModel so all this functionality still exists.

  4. ouldache oussama said

    Thank very match :) very simple and useful

  5. raj said

    thanks for the code. It really helped me save time.

  6. Mubashir said

    Actually i am using

    cmbOrder = new JComboBox(util.GetComboBoxModel(data_CmbVect));

    where util.GetComboBoxModel(data_CmbVect) returns Vector
    and Item_(String comboId, ComboVal)

    please help me how can i use your code in this regard.
    Lots of thanks in advance.

    where data_CmbVect is Vector<Vector> your code is valid for String items

    • Rob Camick said

      It sounds like your data is a Vector of Vectors (which is not a very good design). How does the combo box know which element from the Vector to render? Did you also write a custom renderer? I will assume you second Vector contains String data. If this is what you mean then you need something like:

      SortedComboBoxModel<Vector<String>> model = new SortedComboBoxModel<Vector<String>>(data, customVectorComparator);
      JComboBox<Vector<String>> comboBox = new JComboBox<Vector<String>>( model );

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: