[英]How can I display an ArrayList in a JTextArea
I need to get display the elements of an ArrayList in a JTextArea including a new line after every single element. 我需要在JTextArea中显示ArrayList的元素,包括在每个元素之后的新行。 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? 如何在没有逗号和小点的情况下打印它们?
Try using txtAreaAusentes.append(s.toString());
尝试使用
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());
}
You should replace a
with s
in your output: 您应该在输出中将
s
替换a
:
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
. 还要先构建输出
String
然后将文本区域设置为output
String
。 You can use a StringBuilder
if you want to be more efficient... 如果您想提高效率,可以使用
StringBuilder
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.