quinny
quinny

Reputation: 75

ArrayList output repeating itself?

So i've i'm trying to use a combobox to display different category options for different types of food. Each type of food has its own array list, so vegetables, fruits, dairy, etc. So when the user selects a food category, it will display all the foods from that category in a text area. However, the list keeps repeating itself an extra time whenever a different category is selected. So the first click is fine and will display the food list only once. But if you select a different category after that, the list will repeat itself. Here's the code i'm using right now:

public class foodTypesJFrame extends javax.swing.JFrame {

// Food category arraylists
ArrayList <String> fruitsList = new ArrayList();
ArrayList <String> veggiesList = new ArrayList();
ArrayList <String> dairyList = new ArrayList();



public void foodCategory(String box, String category, ArrayList list)
{
    box = foodCategoryBox.getSelectedItem() + "";

    if (box.equals(category))
    {
        foodOutput.setText("");
        int indexNumber = 0; 
        // Display the different foods
        for (int index = 0; index < list.size(); index++) 
        { 
            indexNumber = index + 1; 
            foodOutput.append(indexNumber + ". " + list.get(index) + "\n"); 
        }
    }
}        

private void categoryBoxActionPerformed(java.awt.event.ActionEvent evt) {                                            

    String foodType = foodCategoryBox.getSelectedItem() + "";

    String fruits = "Fruits";
    String veggies = "Vegetables";
    String dairy = "Dairy";

    Collections.addAll(fruitsList, "Apple", "Orange", "Strawberry");
    Collections.addAll(veggiesList, "Lettuce", "Carrot", "Broccoli");
    Collections.addAll(dairyList, "Milk", "Cream", "Cheese");

    foodCategory(foodType, fruits, fruitsList);
    foodCategory(foodType, veggies, vegetableList);
    foodCategory(foodType, dairy, dairyList);

So with this, if i select the fruits category first, it will output " 1. Apple 2. Orange 3. Strawberry" But if i select the veggies category after that, it will output " 1. Lettuce 2. Carrot 3. Broccoli 4. Lettuce 5. Carrot 6 Broccoli"

Does anyone have any suggestions on how to make it so the list will only display once?

Upvotes: 2

Views: 435

Answers (2)

Yahya
Yahya

Reputation: 14072

Every time you invoke categoryBoxActionPerformed, you're adding the same items to the ArrayLists.

To solve this, you may do one of the following:

  1. Either re-initialize the ArrayLists every time you invoke categoryBoxActionPerformed(), so it looks like this:

     //at the beginning of the method.
     fruitsList = new ArrayList<>();
     veggiesList = new ArrayList<>();
     dairyList = new ArrayList<>();
    
  2. Or wrap it with a boolean:

     boolean firstTime = true; // create a global variable
    

Then wrap the addition:

    if(firstTime){
       Collections.addAll(fruitsList, "Apple", "Orange", "Strawberry");
       Collections.addAll(veggiesList, "Lettuce", "Carrot", "Broccoli");
       Collections.addAll(dairyList, "Milk", "Cream", "Cheese");
       firstTime = false;
    }

Upvotes: 3

Taras Shpulyar
Taras Shpulyar

Reputation: 134

You call Collections.addAll(...) inside the ActionEvent listener this means the each click will add new elements into ArrayList. To fix that You need to initialize all lists in the constructor (which is more correctly in case You have fixed list of products) of your class or to call clear() method before Collections.addAll(...) for each list (in case if you really need to change lists of products inside the event listener)

Upvotes: 0

Related Questions