A default JTable or JList comes with it’s own pre initalized model. Okay – but: how can we modify this model? Which type of model is usually pre initialized?
In the following I’ll just list some of the may possible ways to work with tables and lists and the NetBeans Gui Builder:
1. Create your own Component
After adding the component to the form, go to the “Properties” window/tab and open the “Code” view.
There you can use the “Custom Creation Code” in order to create the JList (for example) the way you want: new JList(new MyDefaultModel()));
If you don’t like to modify the auto created code, you can just set the desired model after letting NetBeans create the List/Table.
3. Use NetBeans to manage it all
Add the table/list to the form, then add a DefaultTableModel/DefaultListModel into the form as well. (Right click in the form, add from palette, beans, choose bean –
enter the full package path to the DefaultTable/ListModel and finished). In the inspector view, you should now see the default table model in
Okay, now attach the model to the table:
Select the JTable/JList – and then you should see an entry “model” in the properties view. Click on the according button , use “Value from existing component”, check “Get Value From:”, check “Property” and select the model you introduced in the above step. – doneAn that’s it. No hand coding needed. If you need this more often you might be quite borded by always entering the full package path. In that case, you can just use the PaletteManager to add all the Default* classes into a new category.
Another possibility is to just get the model after initialization by using getModel(). But well you end up with a regular ListModel which is just an interface and dows not provide any modification. If you rely on what happened before in your code, you could just cast the returned Model into the according DefaultTableModel/DefaultListModel.