I installed my combobox model in the controller class
cboCategory.setModel(new ModernDefaultComboBoxModel(model.getProductCategories()));
productCategories is a List of String . ModernDefaultComboBoxModel is just a model that extends DefaultComboBoxModel .
public class ModernDefaultComboBoxModel extends DefaultComboBoxModel{ public ModernDefaultComboBoxModel(List<String> elements){ super(elements.toArray()); } }
Now in my model productCategories populated from the database in SwingWorker
SwingWorker<Void, String> worker = new SwingWorker<Void, String>() { @Override protected Void doInBackground() throws Exception { //query and resultset stuff while (rs.next()) { publish(rs.getString(1)); } //cleanup stuff } @Override protected void process(List<String> chunks){ List<String> oldCategories = new ArrayList<String>(productCategories); for(String cat : chunks){ productCategories.add(cat); } fireModelPropertyChange(PRODUCT_CATEGORIES, oldCategories, productCategories); } @Override protected void done(){ //some stuff } }; worker.execute();
You see each publish , it raises a property change event for its listener ( fireModelPropertyChange is just a wrapper for firePropertyChange ).
Now in my model listener
@Override public void propertyChange(PropertyChangeEvent evt) { String propName = evt.getPropertyName();
I am stuck in the part where my ModelListener should notify the drop-down list in the view that the data in its model has changed. I have the same situation with JTable , but with JTable I can just call fireTableRowsInserted from my model, which is implemented from AbstractTableModel .
Actually, there is a fireContentsChanged method in AbstractListModel , but unlike JTable , this method is protected, so I canβt access it.
I know that I can just instantiate ModernDefaultComboBoxModel and then call the setModel method of combobox to update combobox, but I'm just wondering if the "clean" way is as clean as JTable