2

i am trying to iterate through JComboBox items i.e its connected to Database, whenever I click it it fetches data from Database and updates it. But its adding duplicate values in such case. I am trying to validate it by iterating through each item once added to JComboBox, if the existing item is similar to item I am trying to add then it shall not add and jump to other statement.

However I am then getting Null pointer error, in order to avoid this error first time I added counter -1, but once items are added and want to update, it gives error.

My JComboBox code is given below:

comboBox.addMouseListener(
            new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent ev) {
                    List<Guest> list = null;
                    list = database.readGuest();
                    int n = list.size();

                    if(n <= 0) {
                        JOptionPane.showMessageDialog(null, "No data found.");
                    }else {
                        for(int count = 0; count < n; count++) {
                            g = list.get(count);
                            String pass = g.getPassportNp();
                            //String s = (String) comboBox.getItemAt(count-1);
                            //for(int i = 0; i < n; ++i) {
                                if(comboBox.getItemCount() != 0) {
                                    if(comboBox.getItemAt(count-1).equals(pass)) {
                                        continue;
                                    }else {
                                        comboBox.addItem(pass);
                                    }
                                }else {
                                    comboBox.addItem(pass);
                                }
                            //}

                        }
                    }
                }
            });

any solution for this?

1
  • How about using a Set as an intermediate or ensure the database doesn't return duplicates? Apr 20, 2016 at 8:45

2 Answers 2

1

You are not iterating over the items in the combobox, but only comparing to the last item in it. Your basic design should be 2 nested for-loops, one for going over your database items and one for going over the combobox items to check if the current database item is already in there. (You may also use List.contains instead of an nested loop which is clearer and shorter).

Side note: registering a mouselistener on the combobox seems like a design smell for this type of work. Fetching items from a database is not something you want to do on the EDT, but rather in a background thread. Also, you don't know what happens first: opening/animating the combobox (done by Swing), or modifying its contents? It makes it hard to think about the control flow, yet another reason to take a different approach.

0

Here is the solution which I figured it out. I hope helps others.

I created a List, and then added all items to it then..

int size = pass.size();
if(comboBox.getItemCount() != 0) {

    comboBox.removeAllItems();
    for(int c = 0; c < size; ++c) {
        comboBox.addItem(pass.get(c));
        }
}else {
     for(int c = 0; c < size; ++c) {
         comboBox.addItem(pass.get(c));
     }
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.