Building Swing apps with Groovy

I’ve been playing around with Groovy for a couple of daya now, and wanted to try out the markup feature in the language to build data with nested structures (XML, HTML etc. The same Builder concept in Groovy applies to Swing apps as well, as a Swing app is constructed from nested components.

Here is a simple Swing app that is a file editor:

<code>
import groovy.swing.SwingBuilder
import java.awt.BorderLayout

def swing = new SwingBuilder()
def fileName

def widget = swing.frame(title:'GroovyEdit', size:[300,200], defaultCloseOperation:javax.swing.WindowConstants.EXIT_ON_CLOSE) 
{
    menuBar()
    {
        menu(text:'File')
        {
            menuItem(text:'New')
            separator()
            menuItem(text:'Save', actionPerformed:
            {
                if(fileName == null)
                {
                    fileDialog = swing.fileChooser()
       	            fileChoice = fileDialog.showSaveDialog()
	            if(fileChoice == fileDialog.APPROVE_OPTION)
	            {
	                fileName = fileDialog.selectedFile.absolutePath
	                println(fileName)
	            }
	        }
	        new File(fileName).withWriter{ w | w.write(editorText.text) }
	    })
            
            separator()
            menuItem(text:'Exit', actionPerformed:{ System.exit(0) })
        }
    }
    panel(layout: new BorderLayout()) {
          editorText = textArea(constraints: BorderLayout.CENTER)
       }
}
widget.show()

</code>

Generating a new Grails app: getting MySQL connection errors

When I ‘generate-all’ on a new app configured to use MySQL in the ApplicationDataSource.groovy file, I am getting some obscure MySQL errors about cannot connect to my server, even though MySQL is running and I can connect with the mysql command line client:

<code>
   [groovy] Caused by: java.sql.SQLException: Communication failure during hands
hake. Is there a server running on localhost:3306?
   [groovy]     at com.mysql.jdbc.MysqlIO.init(Unknown Source)
   [groovy]     at com.mysql.jdbc.Connection.connectionInit(Unknown Source)
</code>

From what I’ve discovered Googling around, this error is because an older MySQL Connector jar is being used to make the connection. I am runing MySQL 4.1 with Connector 3.1.12. Here is a post about the connector jar causing this issue. The solution is to either use the latest Connector jar for MySQL, which I am already doing, or to change the password for the user so that it is stored using the older encryption method, using old_password('password') instead of password('password')

The strage thing is though, that I have placed the latest connector jar into the lib directory in my generated app (I’ve also tried grails/lib even though I’m sure it shouldn’t go there), but it is obviously not using this jar – if I remove it and run again I get the same issue, so it is picking up an older jar on my machine somewhere. – I’m about to do a search and see what turns up (there is nothing in my classpath defined in my Windows env vars).

Update: ok – from doing a file search I solved my problem. I had earlier tried to use an older version of the mysql-connector by mistake before trying the latest version. I had ended up with both versions being beuilt into the web app and the older one was being picked up first. After deleting everything under temp and rebuilding, this solved my problem.