0
votes

I have the following Groovy+SwingBuilder code.

In one panel I generate checkboxes and in another panel I want to access the values of the checkboxes. The code looks basically likes this:

def optionsmap = [ foo : "value_foo", bar : "value_bar"]

SwingBuilder.build()     
{
   frame(title:'demo1', size:[400,700], visible:true, defaultCloseOperation:WC.EXIT_ON_CLOSE) {                
      gridLayout(rows: 1, cols: 2)

      panel(id:'optionPanel', constraints:java.awt.BorderLayout.CENTER) {                        
         gridLayout(rows: 5, cols: 1)                 
         myGroup = buttonGroup();    
         for (entry in optionsmap)
         {         
           checkBox(id: entry.key,   text: entry.value        )                  
         }            
      }

      panel(constraints:java.awt.BorderLayout.WEST) {   

         button ('Show values', actionPerformed: {           
         for (entry in optionsmap)
         {
            println (entry.key as Class).text            
         }           
       })                
    }
  }  
}

optionsmap is a map with (id, text) pairs that can be extended.

When I press show values I get an error message:

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'foo' with class 'java.lang.String' to class 'java.lang.Class'

How could I access the checkboxes for my action in the second panel by using the checkbox ids from optionsmap?

1
What, exactly, do you mean by "does not work"? Does it crash? Does it, perhaps, only output the ones that are checked? Are they all non-selected? How are you binding the data in the previous panel's form? Please be more complete. - billjamesdev

1 Answers

0
votes

The solution to access variables from the map is like this:


                 for (entry in optionsmap)
                 {
                      if (variables[entry.key].selected)
                          println variables[entry.key].text
         }