2

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)}
    }
}
tim_yates
  • 167,322
  • 27
  • 342
  • 338
Bill K
  • 62,186
  • 18
  • 105
  • 157

1 Answers1

3

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)
}
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • Thank you, I should have been able to figure that out but something about the syntax was throwing me. Makes perfect sense! Plus I really love that event map syntax--Have to incorporate that into my regular Groovy syntax – Bill K Jul 07 '15 at 21:06