Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Keeping JComboBox sorted

Posted on 2004-05-02
8
Medium Priority
?
622 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
Technology Partners: 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 200 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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 …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

604 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