Search code examples
groovyswingbuilder

How to dynamically add a button group to panel after panel is built


I am using Groovy Swingbuilder, and I want to dynamically fill a radio button - buttongroup after the initial panel has been built. Example: I have a panel that has a few options. Based on which option is selected I need to fill the button group with a set of radio buttons. The radio options are different for each option.

my panel would look something like this:

panel(id:"chooseClass", visible:true, layout: new BL()){
             vbox(constraints: BL.CENTER){
                 label("Player Statistics", horizontalAlignment: 0)
                 label(id: 'raceLabel', text: raceLabelText, horizontalAlignment: 0)
                 label(id: 'statLabel', text: statLabelText, horizontalAlignment: 0)
                 panel(id:'classGroupPanel', layout: new GridLayout(1,9)){
                    myButtonGroup = buttonGroup(id:'classGroup')
                 }
             }
         }

Then later in my code I have this Method:

def void setClassGroup(){
    def classButtons = plyGen.getAvailibleClass()
    // this is one way I've tried it
            gPane.edt{panel(id:'classGroupPanel', layout: new GridLayout(1,9)){
        buttonGroup(id:'classGroup').with{ group ->
            classButtonGroup.each{ radioButton(id: '${it.name}', CreateRadio("${it.name}"), mnemonic:"${it.mnenomic}", buttonGroup: group)}}
     }
    }
            // and this is another way I've tried it
    gPane.doOutside {
        this.classGroupPanel{ 
        buttonGroup(id:'classGroup').with{group ->
            classButtons.each{ gPane.radioButton(id: '${it.name}', CreateRadio("${it.name}"), mnemonic:"${it.mnenomic}", buttonGroup: myButtonGroup) }
            }   

        }
    }               


}

Both of these attempts compile and run without error, but I don't get a list of radio buttons. I wish there was more documentation for swingbuilder.


Solution

  • Missing panel.revalidate()? This guy shows a radio button group with some songs based on which band you choose. The songs are rendered as radio buttons:

    import groovy.swing.*
    import javax.swing.*
    import java.awt.*
    import java.awt.BorderLayout as BL
    
    def drawSongsPanel(songs) {
      def p
      new SwingBuilder().edt {
        p = panel(visible: true) {
          vbox(constraints: BL.CENTER) {
            buttonGroup().with { btn ->
              songs.each { 
                radioButton text:it, buttonGroup: btn
              }
            }
          }
        }
      }
      p
    }
    
    
    def getSongs(band) {
      switch ( band ) {
        case "stones": return ['start me up', 'jumpin jack flash', 'satisfaction']
        case "beatles": return ['hey jude', 'yellow submarine', 'yesterday']
      }
    }
    
    
    def chooseBand(event, panelBands) {
      panelBands.add drawSongsPanel(getSongs(event.actionCommand))
      panelBands.revalidate()
    }
    
    
    new SwingBuilder().edt {
      frame(defaultCloseOperation: JFrame.EXIT_ON_CLOSE, visible: true, size: [600,500]) {
        panel( visible: true, layout: new BL() ) {
          vbox(constraints: BL.CENTER){
            def panelBands
            panelBands = panel(id:'classGroupPanel', layout: new GridLayout(1,9)) {
              buttonGroup(id: 'classGroup').with { 
                radioButton text:"beatles", buttonGroup: it, actionPerformed:{e->chooseBand(e, panelBands)}
                radioButton text:"stones", buttonGroup: it, actionPerformed:{e->chooseBand(e, panelBands)}
              }
            }
          }
        }
      }
    }