I have a JTextPane which is called outputField, and when the user presses a button called 'list' it will run a function to search through an ArrayList with loops, and display the contents. The issue is I am currently appending each like this:
output = output + " " + passengers.get(x).get(y);
The issue with this is that if I continue to press the 'list' button, it will continue to append the same values over and over again, instead of resetting each time the user presses the 'list' button. I've tried setting
String output = "";
at the start of the function however, it did not work. Here is the whole function:
private void listButtonActionPerformed(java.awt.event.ActionEvent evt) {
String output = "";
passengers.add(firstName);
passengers.add(lastName);
passengers.add(weekOne);
passengers.add(weekTwo);
passengers.add(weekThree);
passengers.add(weekFour);
for (int y=0; y<numOfPas; y++) {
for (int x=0; x<passengers.size(); x++) {
output = output + " " + passengers.get(x).get(y);
}
output = output + "\n";
}
outputField.setText(output);
}
If I have these values in the array, this is the output of 'list'
values
John Doe 100 200 300 400
Jerry Glock 200 400 600 800
output: Run 'list' once:
John Doe 100 200 300 400
Jerry Glock 200 400 600 800
Run 'list' twice:
John Doe 100 200 300 400 John Doe 100 200 300 400
Jerry Glock 200 400 600 800 Jerry Glock 200 400 600 800
etc.
Would like for it to be the Run 'list' once results every time it is run
I have found the answer to my own question! The issue was that I was appending each individual arraylist, to the main arraylist inside the method. I simply changed this:
private void listButtonActionPerformed(java.awt.event.ActionEvent evt) {
String output = "";
passengers.add(firstName);
passengers.add(lastName);
passengers.add(weekOne);
passengers.add(weekTwo);
passengers.add(weekThree);
passengers.add(weekFour);
for (int y=0; y<numOfPas; y++) {
for (int x=0; x<passengers.size(); x++) {
output = output + " " + passengers.get(x).get(y);
}
output = output + "\n";
}
outputField.setText(output);
}
To this:
private void listButtonActionPerformed(java.awt.event.ActionEvent evt) {
String output = "";
for (int y=0; y<numOfPas; y++) {
for (int x=0; x<passengers.size(); x++) {
output = output + " " + passengers.get(x).get(y);
}
output = output + "\n";
}
outputField.setText(output);
}
private void formWindowOpened {
passengers.add(firstName);
passengers.add(lastName);
passengers.add(weekOne);
passengers.add(weekTwo);
passengers.add(weekThree);
passengers.add(weekFour);
}
and it is working flawlessly now.