Reputation: 151
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.
Upvotes: 0
Views: 3466
Reputation: 11
In order to achieve the results you want, you can create a class or function that takes an array as an argument and prints out the items within it in the format you like:
....
public static String printArray (String[] textArray) {
String output = "";
for (String s : textArray) {
output += s + '\n';
}
return output;
}
...
private void displayButtonActionPerformed(java.awt.event.ActionEvent evt) {
// sorts then displays entries in the array
Collections.sort(cds, String.CASE_INSENSITIVE_ORDER);
outputArea.setText(printArray(cds)); //Change this line
}
After adding a similar method to the one I have shown, change the last line in your displayButtonActionPerformed()
method as shown.
I have tested the printArray()
method in a similar example.
Upvotes: 0
Reputation: 22005
Use append
instead of setText
and a loop
for (Object o : cds){
outputArea.append(o + "\n");
}
Upvotes: 2