Reputation: 81
I need to get display the elements of an ArrayList in a JTextArea including a new line after every single element. However, the code by now display all the elements in a single line, separated by commas; and openning and closing brackets "[ ]". The code by now is the following:
public void imprimirLista(ArrayList<Ausente> a) {
for (Ausente s: a) {
txtAreaAusentes.setText(a.toString());
}
}
How can I print them without the commas and brakets?
Upvotes: 0
Views: 4825
Reputation: 54
Try using txtAreaAusentes.append(s.toString());
One thing I'll note is this will append to any existing text that may be in that field. If you want to clear that text, then I would change the function to
public void imprimirLista(ArrayList<Ausente> a) {
StringBuilder sb = new StringBuilder();
for (Ausente s: a) {
sb.append(s.toString());
}
txtAreaAusentes.setText(sb.toString());
}
Upvotes: 1
Reputation: 11973
You can also append the text:
public void imprimirLista(ArrayList<Ausente> a) {
for (Ausente s : a) {
txtAreaAusentes.append(s.toString() + "\n"); // New line at the end
}
}
Upvotes: 2
Reputation: 13222
You should replace a
with s
in your output:
String output = "";
for (Ausente s: a) {
output += s.toString() + "\n";
}
txtAreaAusentes.setText(output);
Also build your output String
first then set the text area to the output
String
. You can use a StringBuilder
if you want to be more efficient...
Upvotes: 0