2

Why does this code fail ? I want to change the color of one panel in a series of several panels, dynamically constructed (total number of panels not known beforehand).

For some reason, this code works when referencing the name of a particular panel (for example 'panel2'), but not when I refer to it dynamically ('panelID').

import groovy.swing.SwingBuilder
import javax.swing.WindowConstants as WC
import javax.swing.JOptionPane
import javax.swing.BoxLayout as BXL

swing = new SwingBuilder()
frame = swing.frame(title:'test',
    defaultCloseOperation:WC.DISPOSE_ON_CLOSE) {

    panel(id:'mainPanel'){
        def panelID 

        (1..6).each {
            panelID = 'panel' + it

            panel(alignmentX: 0f, id: panelID , opaque:true ,background : java.awt.Color.GREEN){
                label('description') 
                textField(id: "description$it", text: panelID, columns: 70 )
                button(id: "button$panelID", text: panelID, actionPerformed : {
                    panelID.background = java.awt.Color.RED
                    panelID.repaint()                       
                })
            }
        }
        boxLayout(axis: BXL.Y_AXIS)

        panel(id:'secondPanel' , alignmentX: 0f){                       
            button('Quit', actionPerformed:{
                dispose()
            })
        }
    }       
}
frame.pack()
frame.show()
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • An advice based on my personal experience with Swing/Swt builders. If you can, do not use them, it seems to be easy in the begging but it is getting hard when you need to do more advance things, use GUI Builder, such as: http://code.google.com/javadevtools/wbpro/ or any other. – Skarab Jun 18 '11 at 16:14

1 Answers1

2

To get the element based on it's ID, you need to access the ID as a parameter of the SwingBuilder, like so:

import groovy.swing.SwingBuilder
import javax.swing.WindowConstants as WC
import javax.swing.JOptionPane
import javax.swing.BoxLayout as BXL

swing = new SwingBuilder()
frame = swing.frame(title:'test', pack:true, visible:true, defaultCloseOperation:WC.DISPOSE_ON_CLOSE) {
    panel(id:'mainPanel'){
        (1..6).each { num ->
            def panelID = "panel$num"
            def pane = panel( alignmentX:0f, id:panelID, background:java.awt.Color.GREEN ) {
                label('description') 
                textField(id: "description$num", text:panelID, columns: 70 )
                button(id: "buttonpanel$num", text:panelID, actionPerformed : {
                    swing."$panelID".background = java.awt.Color.RED
                })
            }
        }
        boxLayout(axis: BXL.Y_AXIS)

        panel(id:'secondPanel' , alignmentX: 0f){                       
            button('Quit', actionPerformed:{
                frame.visible = false
            })
        }
    }       
}
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • @Skarab : I'm new to Groovy and don't know much of Java; I use Groovy as it is the dedicated scripting language of Open Source mind mapping software Freeplane. I'm not clear how I should use a Gui builder inside a groovy script : do I "simply" paste in the resulting code inside the groovy script ? – Michel Parmentier Jun 20 '11 at 00:01
  • @Michel. In this context, maybe GroovyBuilder is the easier solution. I do not know, because I did not write any scripts for Freeplane. I thought that your question refers to the development of UI for your application. – Skarab Jun 20 '11 at 14:14
  • A GUI builder that I mentioned generates Java code. In general, you can mix Java and Groovy code (there are some exceptions, e.g., array definition). – Skarab Jun 20 '11 at 14:18