Does clear use removeAll method for listViews?
I have a program that reads a file and stores the data in multiple arrays. I then use that data from the arrays to display them in a ListView, TextFields, a ComboBox, and a TextArea. I am also using an ActionListener to get the changed event for the ListView Selection. When the changed event fires, it gets the data from the arrays and populates the fields.
The problem is when I save the items to the file, and it reloads the list, it will display the name properly, however the ComboBox, the ID TextBox, and the TextArea is displaying the data from the first item in the listView. This problem goes away when I close and reopen the Application.
Here is some code to review.
Changed Listener
if (SceneController.bugList.getSelectionModel().getSelectedIndex() == -1)
{
SceneController.bugList.getSelectionModel().select(0);
SceneController.textName.setText(names.get(0));
SceneController.statusField.getSelectionModel().select(0);
SceneController.textID.setText(IDs.get(0).toString());
SceneController.textDescription.setText(descriptions.get(0));
}
SceneController.bugList.getSelectionModel().selectedItemProperty()
.addListener(new ChangeListener<String>()
{
@Override
public void changed(
ObservableValue<? extends String> observable,
String oldValue, String newValue)
{
if (!((SceneController.bugList.getSelectionModel()
.getSelectedIndex()) == -1))
{
SceneController.textID.setText(IDs.get(
SceneController.bugList.getSelectionModel()
.getSelectedIndex()).toString());
SceneController.textName.setText(names
.get(SceneController.bugList
.getSelectionModel()
.getSelectedIndex()));
if (status.get(SceneController.bugList
.getSelectionModel().getSelectedIndex()) == false)
SceneController.statusField.getSelectionModel()
.select(0);
else if (status.get(SceneController.bugList
.getSelectionModel().getSelectedIndex()) == true)
SceneController.statusField.getSelectionModel()
.select(1);
SceneController.textDescription
.setText(descriptions
.get(SceneController.bugList
.getSelectionModel()
.getSelectedIndex()));
}
}
});
}
Load List
private static void loadList() throws IOException
{
SceneController.bugList.getItems().clear();
currentID = 0;
InputStream reader = BugTracker.class
.getResourceAsStream("/bugList.txt");
br = new BufferedReader(new InputStreamReader(reader));
String str;
while ((str = br.readLine()) != null)
{
String[] strArr = new String[3];
strArr = str.split(", ");
names.add(strArr[0]);
if (strArr[1].equals("Unsolved"))
status.add(false);
else if (strArr[1].equals("Solved"))
status.add(true);
else
{
System.out.println(strArr[1]);
System.out
.println("Error in Bug List. Please review the File and Check for any Errors.");
}
descriptions.add(strArr[2]);
currentID++;
IDs.add(currentID);
}
SceneController.bugList.setItems((ObservableList<String>) names);
}
Save List
public static void saveList() throws IOException, URISyntaxException
{
URL resource = BugTracker.class.getResource("/bugList.txt");
File file = new File(resource.toURI());
FileOutputStream output = new FileOutputStream(file);
bw = new BufferedWriter(new OutputStreamWriter(output));
for (int i = 0; i < SceneController.bugList.getItems().size(); i++)
{
bw.write(names.get(i));
bw.write(", ");
if (status.get(i) == false)
bw.write("Unsolved");
else if (status.get(i) == true)
bw.write("Solved");
bw.write(", ");
bw.write(descriptions.get(i));
bw.newLine();
}
if (SceneController.statusField.getSelectionModel().getSelectedIndex() == 0)
{
bw.write(SceneController.textName.getText());
bw.write(", ");
bw.write("Unsolved");
bw.write(", ");
bw.write(SceneController.textDescription.getText());
}
else if (SceneController.statusField.getSelectionModel()
.getSelectedIndex() == 1)
{
bw.write(SceneController.textName.getText());
bw.write(", ");
bw.write("Solved");
bw.write(", ");
bw.write(SceneController.textDescription.getText());
}
bw.newLine();
bw.flush();
bw.close();
loadList();
}
GUI, after startup
Before Save
After Save and changed away, then back