I'm creating a medication logger in Swing, which has a custom JPanel for new entries. The panel has a few fields for time and such, one of which being the substance taken, which is just a string from a JComboBox. Substances are to be added through my own menu bar class, which collects a new substance to add from a JOptionPane input dialog. I pass the collected value to the menu bar controller, and from there, I'm not sure how I would both dynamically add it to the JComboList in the new entry JPanel class so it updates the view, and also ensure that the substance is being saved to a file, so I can read that file and populate the JComboBox when opening up the application.
It seems that a possible solution would be to first load any added substances from the file on open into a Set, and then populate the selections in the JComboBox from the values in the Set. Then, when adding a substance from the menu bar, add it to both the JComboBox so the view is current, and the Set the JComboBox was created from, so that upon close, I can simply save the Set using a FileOutputStream.
I'm currently not defining any model for the combo box and instead simply initialize it directly from a (currently hardcoded) set, but it seems like using a MutableComboBoxModel to initialize the JComboBox and add new substances to would help me accomplish this? Is this the correct way to go about this? Or is there a better method I could try?
Thanks for your time.