简体   繁体   中英

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. 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?

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
    }
}

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());
}

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM