Solved

Keeping JComboBox sorted

Posted on 2004-05-02
8
604 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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:mayankeagle
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now