I have multiple jcomboboxes spread throughout my program. Most of these boxes get their data from names or text strings found in databases. Normally when adding a new item, it is added to the bottom of this list.

What I want to do is when I add an element to the combo box is, to insert the element in its sorted alphanumeric order, or insert it at the bottom then move up the list to its appropriate position.... i.e. do an insertion sort one item at a time with an end result of sorted combo box (ascending order).

A typical example of adding an item currently:

                while (rs.next()) {
                    String name = rs.getString("ITEM_NAME");
                    jComboBox2.addItem(name);
                }

Need the code added before the closing brace } and not be a function call.

dont need anything fancy just something short and simple due to the number of combo boxes involved.

Thanks..... my brain is frying, and no eggs to serve them with. lol

I don't see how you would do that.
what you could do, is first read in all the elements, so that you will be able to sort them, and then add the contents as items to your list.

Maybe you can do it by coding your own ListModel then you can maintain the set of values sorted in alpha order. There are a number of list model classes that you could subclass (eg AbstractListModel) to handle the housekeeping

(EDIT) blablablba,

  • lightweight job for SQL interpreter, instead of hard process on the client side

I agree with what I think mKorbel is saying... much better to ORDER the records in your SELECT so they are sorted before you add them to the combo box

I found a bit of code on another site that caught my eye....

Of course it took some modifications and tweeking, but now it works like a charm and very quickly to boot. I thought I would share it with you.....

int count = 0;
int compare = 0;
Object obj;
String name = "";
try {
   conn = DriverManager.getConnection("jdbc:derby:Databases/Equipment_Mundane");
   Statement st = conn.createStatement();
   ResultSet rs = st.executeQuery("select * from EQUIPMENT_MUNDANE");
   while (rs.next()) {
       added = false;
       name = rs.getString("ITEM_NAME");
       count = jComboBox2.getItemCount();
       for (int i = 1; i < count; i++) {
           obj = jComboBox2.getItemAt(i);
           compare = name.compareToIgnoreCase(obj.toString());
           if (compare <= 0) { 
               jComboBox2.insertItemAt(name, i);
               added = true;
               break;
          }
      }
      if (!added) {
          jComboBox2.addItem(name);
      }
   } catch (SQLException ex) {        Logger.getLogger(Spell_Editor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
   }
}

You may notice that the for loop starts at 1 instead of 0:
I manually add a item before entering this routine that displays "Select Item".
By starting the loop at 1 leaves the topmost cell at that position and adds new items from that point forward. If this is not needed then start the loop at 0.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.