Monday, December 10, 2007

Objects Without Object Oriented Development

Groovy's script-like behavior means developers can use classes without having to develop new classes. And Groovy offers a simple, quick way of investigating the components of the Java API such as those in Swing.
import javax.swing.*
import groovy.sql.Sql 
 
// create Swing frame
frame = new JFrame("Simple JTable Example") 
frame.setLayout(new BorderLayout())
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

 
// create Swing table
table = new JTable(6,3)
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF) 
col = table.getColumnModel().getColumn(0)
col.setPreferredWidth(100)
col = table.getColumnModel().getColumn(1)
col.setPreferredWidth(100)
col = table.getColumnModel().getColumn(2)
col.setPreferredWidth(400)
 
int row = 0
 
// access the ol' AS/400 database and populate that table! 
sql = Sql.newInstance("jdbc:as400://theAS400",
                      "UserID", "Password",  
                      "com.ibm.as400.access.AS400JDBCDriver") 
sql.eachRow("select * from mp1heltog.qrpglesrc") {
   table.setValueAt(it.srcdat,row,0)
   table.setValueAt(it.srcseq,row,1)
   table.setValueAt(it.srcdta,row,2)
   row++
}
 
panel = new JPanel()
panel.setLayout(new BorderLayout())
panel.add(table, BorderLayout.CENTER)
 
frame.add(panel, BorderLayout.CENTER)
frame.pack()
frame.setVisible(true)


Object oriented development IS the greatest thing since sliced bread but, not all tasks call for an object oriented solution. No classes are created in the script above; we simply reuse Java's Swing components and the Groovy Sql class. All it takes is a lunch hour.