Solved

Keeping JComboBox sorted

Posted on 2004-05-02
8
613 Views
Last Modified: 2007-12-19
how can i keep the combo box sorted as i add new items?
0
Comment
Question by:abstractionz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10973713
you can search though the box and then use the string compareTo functions to place the items in the correct order
Ghost
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10974362
every time u add it , just put already existing items of Combo to a vector , use Collections.sort and then refill in the JComboBox
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10974363
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Accepted Solution

by:
maheshexp earned 50 total points
ID: 10974395
Vector v = new Vector();
int num = cb.getItemCount();
   
    // Get items
    for (int i=0; i<num; i++) {
        v.add(cb.getItemAt(i));
    }

Collections.sort(v);
cb.removeAllItems();

    for (int i=0; i<num; i++) {
        cb.addItem(v.get(i));
    }

0
 
LVL 16

Expert Comment

by:gnoon
ID: 10974581
Overiding its addItem() method:

JComboBox combo = new JComboBox() {
    public void addItem(Object anObject) {
        int size = (DefaultComboBoxModel) dataModel;
        Object obj;
        boolean added = false;
        for(int i=0; i<size; i++) {
            obj = dataModel.getElementAt(i);
            int compare = anObject.toString().compareToIgnoreCase(obj.toString());
            if(compare <= 0) { // if anObject less than or equal obj
                super.insertItemAt(anObject,i);
                added = true;
                break;
            }
        }
        if(!added) {
            super.addItem(anObject);
        }
    }
};

String strs[] = {"aa","cc","bb","xx","ss"};
for(int i=0; i<strs.length; i++)
    combo.addItem(strs[i]);
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10974662
If you follow gnoon's approach, which is pretty neat and clean, you need to either provide the same implementation for all your combo-boxes, or you need to make a class which extends JComboBox and over-ride the addItem () method. However, if you don't want to extend JComboBox, then I would go by maheshexp's approach - but why not use an ArrayList instead of a Vector because its faster.
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10974771
>but why not use an ArrayList instead of a Vector because its faster
i agree!!!!, it's just a demo that's it
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 11012663
thanks :-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hashmap order 17 62
import as existing maven project 3 77
JDBC performance 8 33
How do i find out all classes which contain nested class 2 14
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.
Suggested Courses

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question