I'm creating a menu by reading a directory off the disk and creating a menuItem for each file in that directory.
I'd like to have it re-read the disk every time that menu is clicked.
SwingBuilder doesn't seem to want to do this-- at least not easily.
Do I have to add a closure to the "Menu" that creates the MenuItems using old-school swing (I don't even know if this would work since you've already clicked the menu), or is there some trick to get SwingBuilder to re-evaluate a section whenever it is entered?
Here's what I have now:
File scriptDir = new File("C:/myBatchFiles")
menu(text:"External tools", visible:scriptDir.isDirectory()) {
scriptDirlistFiles().each{
File oneItem ->
String name = oneItem.name
String command = '"' + scriptDir.path + '/' + name + '"'
menuItem(action(name:name){MyUtils.cmd(command)}
}
}
You should be able to adapt the following:
import groovy.swing.*
import javax.swing.event.*
new SwingBuilder().edt {
def fillMenu = { ->
scripts.removeAll()
new File('/tmp').listFiles().each {
scripts.add(menuItem(text: it.name))
}
}
frame(title: 'Testing', size: [800, 600], visible:true) {
menuBar {
menu(id:'scripts', text: 'External Scripts')
}
}
scripts.addMenuListener([ menuCanceled: { e -> },
menuDeselected: { e -> },
menuSelected: { e -> fillMenu() } ] as MenuListener)
}