groovyswingbuilder

Get id from Swing component in Groovy SwingBuilder


Let's say I declare an object using SwingBuilder as follows:

swing.button(id:'something')

I know I can get the Component back from SwingBuilder by calling:

swing."something"

But how can I go from an instance of the Component, back to its id as a String?

Component c = getMyButton()
String whatIWant = c.id    //Property doesn't exist
String attempt2  = c['id'] //Property doesn't exist

Solution

  • I believe what you are looking for is the name, so Component#getName() should do what you want.

    For example:

    Component c = getMyButton()
    String whatIWant = c.getName() // or c.name
    

    Unfortunately, this is not documented, but it appears to be what the Groovy SwingBuilder source does, though perhaps only when there is no explicit .id property (one does not exist on Component..?)