How to dynamically add a typed item in an editable Jcombox to a pre existing ArrayList defining the combobox
//This is the block of Code for the JSwing form with Jcombobox on it and having an ArrayList pointed to the comboBox
ArrayList<Chemicals> chemicals; /** * Creates new form EditChemical */ public EditChemical() { initComponents(); chemicals = new ArrayList<Chemicals>(); populateArrayList(); String[] chemicalsArray = new String[chemicals.size()]; for (int i=0 ;i<chemicals.size(); i++) { chemicalsArray[i] = chemicals.get(i).getChemName(); ChemNameCombo.setModel(new javax.swing.DefaultComboBoxModel<>(chemicalsArray)); } ChemNameCombo.setSelectedItem(""); } public void populateArrayList(){ try{ FileInputStream file = new FileInputStream("Chemicals.vic"); ObjectInputStream inputFile = new ObjectInputStream(file); boolean endOfFile = false; while(!endOfFile){ try { chemicals.add((Chemicals) inputFile.readObject()); } catch (EOFException e){ endOfFile = true; } catch (Exception f){ JOptionPane.showMessageDialog(null, f.getMessage()); } } inputFile.close(); } catch(IOException e) {JOptionPane.showMessageDialog(null, e.getMessage());} }}
And this is the Model Class for the ArrayList
public class Chemicals implements Serializable { private String ChemName; private String ChemCategory; public Chemicals(String ChemName, String ChemCategory){ this.ChemName = ChemName; this.ChemCategory = ChemCategory; } public void setChemName(String ChemName) { this.ChemName = ChemName; } public void setChemCategory(String ChemCategory) { this.ChemCategory = ChemCategory; } public String getChemName() { return ChemName; } public String getChemCategory() { return ChemCategory; } }
I believe I am supposed to work with the ChemNameComboItemStateChanged portion of code, but I do not know how to go about it
Thank you in Anticipation