[groovy-user] Re: SwingBuilder / Binding of ComboBox items does not work - empty

203 views
Skip to first unread message

aalmiray

unread,
Sep 7, 2010, 3:26:55 AM9/7/10
to us...@groovy.codehaus.org

You cannot bind to the items: property ^, however you can bind to elements.

import groovy.beans.Bindable;
import groovy.swing.SwingBuilder
import javax.swing.WindowConstants as WC
import java.awt.BorderLayout as BL

@Bindable class Model{
List comboBoxValues = ['1','2','3']
}

Model m=new Model()
swing = new SwingBuilder()
frame = swing.frame(size:[200, 100],title:'Enviroment
Helper',defaultCloseOperation:WC.EXIT_ON_CLOSE) {
borderLayout()
c1 = comboBox(items:model.comboBoxValues,actionPerformed: {println it},
constraints: BL.NORTH)
comboBox(items:m.comboBoxValues,actionPerformed: {println it},
constraints: BL.SOUTH)
noparent {
bean(c1, elements: bind {model.comboBoxValues})
}
}
frame.show()
--
View this message in context: http://groovy.329449.n5.nabble.com/SwingBuilder-Binding-of-ComboBox-items-does-not-work-empty-tp2804857p2805737.html
Sent from the groovy - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


aalmiray

unread,
Sep 9, 2010, 8:30:25 AM9/9/10
to us...@groovy.codehaus.org

OK, I believe the problem is a misunderstanding of what @Bindable does when
paired with Lists. @Bindable will trigger an event whenever the value of the
annotated property values. In your case you're not changing the value but
adding a new item, so no event will be fired.

Ad up to the fact that items: copies the values form the original collection
then even changing the value of m.comboBoxValues will accomplish nothing.

What you can do is listen to changes on the List itself. You can either use
GlazedLists' EventList and its companion models (a great choice btw) or
Groovy's own ObservableList, like this

import groovy.swing.SwingBuilder
import groovy.beans.Bindable
import java.beans.PropertyChangeListener

class Model{
@Bindable ObservableList comboBoxValues = ['1', '2', '3'] as
ObservableList
}

def m = new Model()
new SwingBuilder().edt {
frame(title: 'Foo', pack:true, visible: true) {
borderLayout()
comboBox(id: 'c1', constraints: CENTER, items: m.comboBoxValues,
actionPerformed: {println c1.selectedItem})
textField(columns: 20, constraints: NORTH, actionPerformed: {e ->
m.comboBoxValues << e.source.text})
noparent {
m.comboBoxValues.addPropertyChangeListener({e ->
// update the underlying ComboBoxModel
switch(e.typeAsString) {
case 'ADDED': /*handle new item*/ break;
case 'REMOVED': /*handle item deleted*/ break;
case 'UPDATED': /*an item changed internally*/ break;
case 'CLEARED': /*all items are gone*/ break;
case 'MULTI_ADD': /*several items added*/ break;
case 'MULTI_REMOVE': /*several items removed*/ break;
}
} as PropertyChangeListener)
}
}
}

Cheers,
Andres
--
View this message in context: http://groovy.329449.n5.nabble.com/SwingBuilder-Binding-of-ComboBox-items-does-not-work-empty-tp2804857p2827393.html

Reply all
Reply to author
Forward
0 new messages