3
ArrayList <String> cdcollection = new ArrayList();

private void initButtonActionPerformed(java.awt.event.ActionEvent evt) {
    Collections.addAll(cdcollection, "small", "mayre", "brown", "evner", "rain" );
    initButton.setEnabled(false);

}

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

          for (int i = 0; i < cdcollection.size(); i++)  {
          mainTextArea.setText(cdcollection.get(i));
    }
}

private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
    cdcollection.add(cdtitleInput.getText());
}

private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {
    cdcollection.remove(cdcollection.size()-1);
}

When I run this and click the display button only the last cd title (rain) appears... How can I get all five cd titles to appear each on one line?

3 Answers 3

8

Use Append() instead of setText()

Sign up to request clarification or add additional context in comments.

1 Comment

You can do line splits with \n . If you have to, you can also use setText() to clear the box, before using append().
0

Do changes in displayButtonActionPerformed method as shown below.

private void displayButtonActionPerformed(java.awt.event.ActionEvent evt) {    
    String str = "";
    for (int i = 0; i < cdcollection.size(); i++)  {
          str = str + cdcollection.get(i);
    }
    mainTextArea.setText(str);
}

2 Comments

Thanks, this works better because append affects the add button by adding the whole collection when pressed.
Simplified for-loop isn't for you?
0

Use the simplified for-loop.

private void displayButtonActionPerformed (ActionEvent evt) {    
    for (String cd : cdcollection) {
          mainTextArea.append (cd + "\n");
    }
}

It will often prevent off-by-one errors, it is often shorter and it is better readable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.