Code:
ArrayList <Integer> marks = new ArrayList();
private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
Collections.addAll(marks, (Integer.parseInt(markInput.getText())));
marks.add(Integer.parseInt(markInput.getText()));
for (int j =0; j < marks.size(); j++) {
markdisplayTextArea.setText(Integer.toString(marks.get(j)));
}
This program is supposed to display each number on a new line in a JTextArea when the user types it in and presses the “add” button.
It displays each number but only one at a time. I want each number entered to be displayed on a new line in ascending. So if the user entered 4 numbers, say, 78, 92, 54, and 21, I want them displayed like this:
21
54
78
92
You are resetting the text in every single loop in the last loop of the code.
Just write a loop to concatenate the numbers into one String (adding
\nafter every number), then set the text ofJTextAreato the concatenated result. SinceJTextAreasupports multiline text, it should be displayed correctly.