Solved

Keeping JComboBox sorted

Posted on 2004-05-02
8
611 Views
Last Modified: 2007-12-19
how can i keep the combo box sorted as i add new items?
0
Comment
Question by:abstractionz
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Suggested Solutions

Title # Comments Views Activity
Groovy problem when using SOAPUI : DispatchException occurred 7 60
JavaFX TableView not displaying correctly 3 58
Java basic valueOf question 1 29
jsp error 6 26
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

830 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