0

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.

Tree
  • 41
  • 4
  • Does this answer your question? [Dynamically adding items to a JComboBox](https://stackoverflow.com/questions/7387299/dynamically-adding-items-to-a-jcombobox) – DevilsHnd - 退職した Dec 08 '21 at 20:46
  • You have at least two questions. But, yes, if you want to update a `JComboBox`, then you need to update the `ComboBoxModel`, take a look at [`DefaultComboBoxModel`](https://docs.oracle.com/javase/10/docs/api/javax/swing/DefaultComboBoxModel.html) as starting point as this can do it for you. Personally, when you add a new substance, I would save it to the "data source" first, I would then use some kind of observer pattern to generate a notification that the data source has been modified in some way so that interested parties can take appropriate actions. – MadProgrammer Dec 08 '21 at 20:52
  • This suggests that either the "substance data store" needs to be a singleton or injected directly into all parts of the app that may need to use it (ie dependency injection). Pros and cons to either approach. And any case, the question is to broad and any answer would be vague and overly generalised – MadProgrammer Dec 08 '21 at 20:53
  • @MadProgrammer I'd like the data source to simply be "substances.txt" or something to that effect. So from what I understand, I could make the string Set of substances observable, thus if I add a substance to that Set, I could update both the `ComboBoxModel` as well as my saved "substances.txt" file with the new substance, given both are observers to the Set? Or do you mean that the "substances.txt" should be the observable? Apologies for perhaps simple questions, I'm new to Swing. – Tree Dec 08 '21 at 21:15
  • 1
    Conceptually, this isn't a Swing issue, this is a basic "design" issue (which is why software engineering has some core design patterns). The "data store" or "data manager" would be observable, but it would be responsible for keeping the physical store up-to-date as well as managing the in memory store. How this is done is implementation detail and for the most part, consumers of the service, won't care – MadProgrammer Dec 08 '21 at 21:38

0 Answers0