Solved

Keeping JComboBox sorted

Posted on 2004-05-02
8
615 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
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!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

705 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