Thursday, February 11, 2010

Add Rows to Groovy's Swing Builder DefaultTableModel

If you've had an opportunity to mess around with Groovy's Swing Builder, you may have noticed that the table model used for tables is not the standard javax.swing.table.DefaultTableModel, but is an instance of groovy.model.DefaultTableModel. Java's implementation of the DefaultTableModel has an addRow() method that allows the addition of new rows, however Groovy's implementation does not.

This left me, and many others, wondering how to go about adding a new row to Groovy's DefaultTableModel object. After looking over the Groovy API, I have come up with the solution below. Enjoy:

import groovy.swing.SwingBuilder

def swing = new SwingBuilder()
def tableData = [ [firstName: "Bob", lastName: "Stevens"], [firstName: "Lucy", lastName: "House"] ]
def myTable = swing.table(){
tableModel( list:tableData ){
propertyColumn( header:"First Name", propertyName:"firstName" )
propertyColumn( header:"Last Name", propertyName:"lastName" )
}
}

// Let's add a new row to the table
def rows = myTable.getModel().getRowsModel().getValue()
rows.add( [ firstName: "Reese", lastName: "Smith" ] )
myTable.getModel().getRowsModel().setValue( rows )
myTable.getModel().fireTableDataChanged()

This example will add a new row to the end of the table and then redraw the JTable so the new row will show up. The function getRowsModel() returns the ValueModel which allows you to manipulate the data within the table. The value set within the ValueModel will be a list of the rows in the table and you can add rows by calling add( rowData ) or specify an index with add( index, rowData ) to allow you to insert data at a specific spot in the list.

Tuesday, February 2, 2010

Fix Slow Charter DNS Lookups by Using OpenDNS

I have been having issues with DNS resolution being extremely slow with Charter Communications Internet on my Macbook. I tried disabling IP v6 in Firefox and that seemed to work for a little while, but eventually my DNS lookups slowed down again. Frustrated, I began looking for ways to not use Charter's DNS servers.

I found out about OpenDNS after googling a little bit. I opened up a free account and switched my router over to their DNS servers. Low and behold, my Internet speed is blazing again on my Macbook. So, if you're having issues with slow DNS lookups on OSX and Charter is your ISP, give OpenDNS a try.

© 2010 Confessions of a Java Programmer, All Rights Reserved