2012-12-15 156 views
3

我正在使用Groovy Swingbuilder,並且想要在初始面板構建完成後動態填充單選按鈕 - buttongroup。例如:我有一個有幾個選項的面板。根據選擇哪個選項,我需要用一組單選按鈕填充按鈕組。每個選項的收音機選項都不相同。如何在構建面板後動態地將按鈕組添加到面板

我的面板會是這個樣子:

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') 
       } 
      } 
     } 

再後來在我的代碼我有這樣的方法:

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) } 
      } 

     } 
    }    


} 

這兩種嘗試編譯和運行沒有錯誤,但我不」 t獲取單選按鈕列表。我希望有更多關於swingbuilder的文檔。

回答

3

失蹤panel.revalidate()?這個傢伙根據你選擇的樂隊顯示一個單選按鈕組。歌曲呈現爲單選按鈕:

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)} 
      } 
     } 
     } 
    } 
    } 
} 
+0

對不起,花了這麼長的時間才能接受這個答案。忙於其他事情。這個例子很好地指出我如何實現我所需要的。謝謝你會 – TroyB

+0

哇,我沒有記得這個問題! :-D。很高興幫助 – Will

+0

這是一個非常有幫助的問答,我很驚訝這個答案自2012年以來有0票(和問題1)! +1 –