0

I'm working on a small pet project, and I ran into a strange issue with my ComboBox. The intended functionality of this part of the program is for an item to be added to the ArrayList that fills the comboBox(which is converted to an array for that purpose), written to the file, and then have the file read again to update the list in the combobox. For reasons I don't understand, it doesn't change dynamically, and repaint didn't work. It does, however, reflect the change if I close and reopen the program, when the constructor deserializes the file that was updated. Here's the code:

Edit: I attempted the revalidate method, same behavior continues. While the program is open, if I add an item, it doesn't show in the list, and the rest of the program behaves as though the item I added isn't there, even throws null pointers with attempted references to it. After closing the program and reopening it, the item that was added on the previous run is present in the list, as it was supposed to be, but then the removal method doesn't work, and the item won't delete while the current run of the program is active.

Edit 2: The code for my comboBox, and the inclusion of the revalidate/repaint methods in the place where they were both called. I've tried each individually, as well as both, the behavior remains the same

JComboBox comboBox = new JComboBox(itemList.toArray(new Item[itemList.size()]));

btnNewButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        Item newItem = new Item();
        newItem.setItemName(JOptionPane.showInputDialog("Please enter the name of the site or service this login is for."));
        newItem.setUserName(JOptionPane.showInputDialog("Please enter your username for this site or service"));
        newItem.setPassWord(JOptionPane.showInputDialog("Please enter the password you use for this site or service"));
        itemList.add(newItem);
        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(passListFile));
            oos.writeObject(itemList);
            oos.close();

            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(passListFile));
            itemList = (ArrayList<Item>) ois.readObject();
            ois.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            comboBox.revalidate();
            comboBox.repaint();
    }
});
  • The code you have shared is definitely useful, but can you please add more like where you have the combobox? Where do you repaint? Please tag me later so I do not forget this question. – Koray Tugay Dec 24 '17 at 13:06

1 Answers1

0

Figured it out myself, I needed to update the Default Model to the arraylist, like so

comboBox.setModel(new DefaultComboBoxModel<Item>(itemList.toArray(new Item[itemList.size()])));
                    comboBox.getParent().validate();