Search code examples
javaarraysswingjframejtextarea

TextArea output from a List


I am having trouble with modifying output from a List (called "cds") inside a JTextArea (from JFrame)......

When I run displayButtonActionPerformed, it places all objects in the array into the JTextArea.

However, these objects are strung together with commas in one big list.... Is there any code which would remove the commas, and create a newline between each object.....

The array can be arbitrarily large, so simply doing a collections.size(0) then /n then collections.size(1) and then /n........will not work.

My code is as follows:

private void displayButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
    // sorts then displays entries in the array
    Collections.sort(cds, String.CASE_INSENSITIVE_ORDER);
    outputArea.setText(cds.toString());
}

The line in question is:

outputArea.setText(cds.toString());

This is what they DO look like in the JTextArea:

[Abbey Road -- Beatles, Alive -- Doors, Gimme Shelter -- Rolling Stones, Hey Jude -- Beatles, Staying Alive -- Beegees]

This is what they SHOULD look like in the JTextArea:

Abbey Road -- Beatles
Alive -- Doors
Gimme Shelter -- Rolling Stones
Hey Jude -- Beatles
Staying Alive -- Beegees

P.S., I'm not having trouble currently removing the brackets, but if anyone knew a simple way, that'd be great too.


Solution

  • Use append instead of setText and a loop


    Solution

    for (Object o : cds){
        outputArea.append(o + "\n");
    }
    

    Output

    enter image description here