1

I have a panel in a griffon view

MyAppView.groovy

panel(id: 'tabpanelWest', constraints: WEST, border: emptyBorder(6)) {
        boxLayout(axis: BoxLayout.Y_AXIS)
        button('Add', actionPerformed: controller.addToggleButton)
        toggleButton(new JToggleButton('Secretaría',imageIcon('/onbutton.png'), true), itemStateChanged: controller.toggleRly)
        toggleButton(new JToggleButton('Hall Entrada',imageIcon('/onbutton.png'), true), itemStateChanged: controller.toggleRly)
}

In my MyAppController.groovy I have the next closures:

    def toggleRly = { evt ->
        def toggleButton = evt.source
        def status = toggleButton.isSelected() ? 1 : 0
        // Calls to a service which does some stuff
        URL url = status ? app.getResourceAsURL('onbutton.png') : app.getResourceAsURL('offbutton.png')        
        toggleButton.setIcon(new ImageIcon(url))
        toggleButton.setSelected(status ? true : false)       
  }

  def addToggleButton = { evt ->
      execInsideUIAsync {
        URL url = app.getResourceAsURL('onbutton.png')
        def panel = view.tabpanelWest
        def jtogglebutton = new JToggleButton('Secretaría',new ImageIcon(url), true)
        panel.add(jtogglebutton) 
        panel.revalidate()
      }
 }

I want to add dynamically toggle buttons to my panel when the user submits a form which is going to be part of the UI. In the above example i use a simple button.

The addToogleButton closure adds a JTogglebutton but I do not know how can i attach a listener such as the one of the view "itemStateChanged: controller.toggleRly)" so that when it is clicked my controller closure toggleRly is called.

I have tried unsuccessfully with

panel.add(new SwingBuilder().edt {toggleButton(jtogglebutton, itemStateChanged: view.controller.toggleRly)})
4

1 回答 1

1

You can use SwingBuilder DSL for components directly (JToggleButton)

Try this:

def addToggleButton = { evt ->
      edt {
        def panel = view.tabpanelWest
        def jtogglebutton = toggleButton(text:'Secretaria', selected:true, itemStateChanged: toggleRly)
        panel.add(jtogglebutton)
        panel.revalidate()
      }
 }
于 2012-05-30T14:09:45.647 回答