Swing Tutorial in Matisse - Custom Table Models
By johnc on May 11, 2006
The continuing adventures of the Swing Tutorial in NetBeans and Matisse. In yesterday's blog, I talked about how it's a pain that when you define the data for your JTable's model in some defined arrays of strings and objects, the data doesn't show up in the design view.
In real-world applications, however, you're most likely going to define your custom TableModel to do things like access a database. For example, TableDemo.java sets up MyTableModel as an inner class. So how do you get this set up in the IDE?
Once again, we're using the model property editor for the JTable (select the JTable - not the JScrollPane - in the Inspector window and click the button for the model property in the Properties window). One simple way to do this is to switch to Form Connection mode, select User Code, and just enter new MyTableModel(). If you run the file, you'll see the model is properly used. But in the design view, we still see nothing.
A better way is to copy the MyTableModel class to its own separate class, called MyTableModel, of course. Then build the project (you need a binary version of MyTableModel for what we're about to do). Now select MyTableModel in the Projects window and press Ctrl-C, then paste it anywhere in the design view (or select any node in the Inspector and paste). An instance of MyTableModel gets added to the Other Components node.
Now we can go back to the model property editor for the JTable, select From Bean, and select myTableModel1. And voila, the data appears in the design view.
This and much more is coming up in an excellent tutorial being prepared by Talley Mulligan on hooking up a Matisse GUI form to a DB back-end, so stay tuned for the full story.