'Having trouble displaying updating info in JTextField

My assignment is to make a favorite ice cream flavor tracker, and I'm having issues with a custom entered answer (one that isn't already listed to vote on). This is expected to display the entered text and the number of times that it's been entered previously (if applicable). (ex: Pistachio: 4, Peanut Butter: 3, Cheesecake: 1)

I've managed to write the code that keeps track of all entered flavors as well as the count of times they've been entered. My struggle is displaying all the information at once. I want it to show the previous entries and their counts, while also displaying any new ones at the same time. My code will only show the most recently entered entry, and its count if it's previously been entered.

What I need:


Cookie: 4, Cherry: 2, Oreo: 1, Rainbow Sherbert: 2,

What I'm getting:

(It's only printing the most recent input not all of them)

(Note the most recent input does not need to be at the top, displays in order of first added)


Rainbow Sherbert: 2,

Here's the code I have:

Below are the integers and strings used to store the values, the array that stores the entered text, and I use .frequency to track the count. My issue lies in the final else-if statement.

static int totalSubmissions = 0;  // tracks # of custom submissions (used to navigate Array List)
    static String printList = "";
    static ArrayList<String> userInputs = new ArrayList<String>(); // contains strings (not listed ice creams) from user input
    static JLabel textAreaTitle = new JLabel("Unlisted Flavors: ");
    private static TextArea pollOtherLabel = new TextArea();

// Custom input and submit button
    private static JTextField customInput = new JTextField();
    private static JButton submitInput = new JButton("Submit");

private static class FlavorListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            // checks vote chocolate and contains output
            if(e.getSource() == voteChocolate){
                chocolateVotes++;
                pollOption1Label.setText("Chocolate Votes: " + chocolateVotes);
            } else if (e.getSource() == voteVanilla){ // checks vote vanilla and contains output
                vanillaVotes++;
                pollOption2Label.setText("Vanilla Votes: " + vanillaVotes);
            } else if (e.getSource() == voteStrawberry){ // checks vote strawberry and contains output
                strawberryVotes++;
                pollOption3Label.setText("Strawberry Votes: " + strawberryVotes);
            } else if (e.getSource() == submitInput){ // checks custom input and contains output
                userInputs.add(customInput.getText());
                String currentString = customInput.getText();
                for (int i=0; i<userInputs.size(); i++){
                    printList =  userInputs.get(i) + ": " + Collections.frequency(userInputs, currentString) + ", \n"  ;         }
         //printList = currentString + ": " + Collections.frequency(userInputs, currentString) + ", ";
          //(Different attempt of mine ^^)
                customInput.setText("");
                pollOtherLabel.setText(printList);
                totalSubmissions++;
            }
        }
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source