【问题标题】:populate a comboBox in Griffon App dynamically动态填充 Griffon App 中的组合框
【发布时间】:2010-02-21 06:48:36
【问题描述】:

我的 Griffon App(或 groovy swingBuilder)视图中有 2 个组合框

country = comboBox(items:country(), selectedItem: bind(target:model, 'country', 
            value:model.country), actionPerformed: controller.getStates)

state = comboBox(items:bind(source:model, sourceProperty:'states'), 
                   selectedItem: bind(target:model, 'state', value:model.state))

控制器中的 getStates() 根据所选国家/地区填充模型中的 @Bindable List states = []。

上面的代码没有给出任何错误,但从未填充过状态。

我将状态从 List 更改为范围对象(虚拟),它给了我一个错误 MissingPropertyException No such property items for class java.swing.JComboBox。

我在这里遗漏了什么吗?在 Nabble 上有几个与此相关的条目,但没有什么是清楚的。如果我有一个标签而不是第二个组合框,上面的代码就可以工作。

【问题讨论】:

    标签: groovy combobox griffon swingbuilder


    【解决方案1】:

    我相信 items: 属性是不可观察的,它仅在构建节点时使用。通过在模型上设置绑定或使用 GlazedLists 的 EventList,您可能会获得更好的结果。

    【讨论】:

    • 从我读到的 items 属性没有被绑定为源。源只会在整个集合更新时触发更新,即 model.states = ['TT', 'CX'] 如果您想触发列表的修改,请使用可观察列表并绑定到可观察列表。
    【解决方案2】:

    型号:

        @Bindable String country = ""
        EventList statesList = new BasicEventList()
    

    控制器:

        def showStates = { evt = null ->
        model.statesList.clear()
        def states = []
        if(model.country == "US") 
                     states = ["CA","TX", "CO", "VA"]
        else if(model.country == "Canada")
             states =  ["BC", "AL"]
        else
              states = ["None"]
    
        edt {model.statesList.addAll(states.collect{it})}
        }
    

    查看:

        def createComboBoxStatesModel() { 
                       new EventComboBoxModel(model.daysList) }
    
        comboBox( items:["USA","Canada","other"], selectedItem: bind(target:model, 'country', value: model.country), actionPerformed : controller.showStates)
    
        comboBox( model: createComboBoxStatesModel(), selectedItem: bind(target:model, 'state', value:model.state))
    

    【讨论】:

    • 我认为'createComboBoxStatesModel'闭包中的'model.daysList'应该是'model.statesLlist'?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    • 2020-09-03
    • 2010-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多