Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Each time I click on the JCombobox's arrow to show the list of exercises, it will continue to populate the combo box with duplicates entries shown in the image, made code is also inserted. I know its to know with the re-clicking of the arrow due to the popupmenulistener, but unsure how to correct this if someone could help -

Java
<pre>       
exerciseList.addPopupMenuListener(new PopupMenuListener() {
            @Override
            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                ArrayList<String> nameList = new ArrayList<>(getComboValues());
                
                for (String name : nameList) {
                    exerciseList.addItem(name);
                }


            }

            @Override
            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            }
            @Override
            public void popupMenuCanceled(PopupMenuEvent e) {

            }
        });
    }

    public ArrayList<String> getComboValues () {

        ArrayList<String> exerciseNames = new ArrayList<>();

        try {
            Connection connection = SQLConnection.connection();
            Statement statement = connection.createStatement();
            String select = "SELECT ExerciseName FROM exercises ORDER BY ExerciseName ASC";
            ResultSet rs = statement.executeQuery(select);

            while (rs.next()) {

                exerciseNames.add(rs.getString("ExerciseName"));

            }
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
        return exerciseNames;
    }
}


What I have tried:

Tried using break within the for loop but it just shows one of the values from the database, understandably. But each time I click on the combobox, it continues to fill it with duplicates of the value after break was implemented.
Posted
Updated 21-Nov-22 0:01am
Comments
[no name] 19-Nov-22 23:27pm    
So maybe you need to do it once?(like application on load) and not by each click event? Your code adds new element to existing context, so no surprise.

You may also call `exerciseList.removeAllItems()` before adding items, and I think this will break current state(selected index) of the combo.
Marc Brolly 21-Nov-22 5:59am    
Calling the `exerciseList.removeAllItems()` before the for loop fixed the issue for me, thanks for the help!
[no name] 20-Nov-22 11:48am    
if exerciseList.Count / Length > 0, don't load it again.

1 solution

Question was resolved by adding exerciseList.removeAllItems(); before the for loop

Java
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                ArrayList<String> nameList = new ArrayList<>(getComboValues());

                exerciseList.removeAllItems();
                for (String name : nameList) {
                    exerciseList.addItem(name);
                }
            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900