javastringjlistdefaultlistmodel

Displaying an Entire JList to a JTextArea


I am currently working on a point-of-sale system that has a JList that displays everything the customer purchased. After ending the transaction, I want to display the contents of the entire list into a JTextArea. I use the following code:

String s = listModel.toString();
jTextArea.append(s);

The JTextArea displays an odd-looking set of codes rather than printing the contents of the list.

I have read other articles related to my problem, but all articles that I have read only provides answers for printing a single item from the list but not all. Thank you everyone!


Solution

  • Take the model and append each element to the JTextArea

    for (int index = 0; index < listModel.getSize(); index++) {
        jTextArea.append(model.getElementAt(index).toString());
    }