Doogledude123 45 Posting Whiz

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
060e6021f1973d99dd98d9a9093dd4e6

Before Save 8134a1c8e94b8128bedb5341e2984792

After Save and changed away, then back 3e0b451d320762011107743c82f38c70

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.