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.