« Return to Thread: SwingBuilder/Griffon how to bind items/model to combobox?

Re: SwingBuilder/Griffon how to bind items/model to combobox?

by bubbles.way :: Rate this Message:

Reply to Author | View in Thread

Another solution is to make ComboBoxModel part of the griffon model and use it for source binding:

AppModel.groovy:

class AppModel {
  ...
 @Bindable ComboBoxModel toModel
}


Bind it in view:
AppView.groovy

comboBox( 'model': bind {model.toModel})

Then, in controller you can change combobox model like this:

AppController.groovy:
model.toModel = new DefaultComboBoxModel(['A', 'B', 'C'] as Object[])

This does not require to have access to combobox object in controller. Unfortunately your model class has to be aware of Swing  ComboBoxModel class, which is not so clean solution.

Another problem I have found  is when trying to bind selected item.
It looks like you can bind it as source  (change model -> change selection in combobox) or as target (change selection in combobox -> change model), but not as both.


AppView.groovy:
selectedItem: bind(target: model, targetProperty: 'to', value: model.to))

or

selectedItem: bind(source: model, sourceProperty: 'to', value: model.to))

but in case you use:

selectedItem: bind(source: model, sourceProperty: 'to', target: model, targetProperty: 'to', value:
model.to))


only source is  working.

As a workaround you have to remember combobox (again!) and perform update of selected item in controller.

AppController.groovy:
model.to = 'A'
toCombo.selectedItem = model.to // workaround


 

 « Return to Thread: SwingBuilder/Griffon how to bind items/model to combobox?