How to use TableModels and ListModel with NetBeans GUI Builder

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()));

2. Set Model manually

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.

Almost same as above, you can either add a custom Post-Creation or Post-Init Code. Of course you could also just modify the constructor and add your code after the call to initialize().

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
“Other components”.

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.

4. Cast …

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.

4 thoughts on “How to use TableModels and ListModel with NetBeans GUI Builder”

  1. Hi, This was of great help. Thanks.
    If possible, please share on how one can dynamically (at runtime)
    attach a customized TableModel to a Jtable in Netbeans.

    1. Hi Muhindi,
      Step 2: “Set Model manually” woulld be your favourite in that case, wouldn’t it? Or where exactly is the problem with this? If you can explain me more detailled what the problem is, I might be able to extend the guide accordingly.


  2. “choose bean –
    enter the full package path to the DefaultTable/ListModel and finished). ”

    Just what the hell is the “full package path to the DefaultTable/ListModel”
    Was I supposed to be born with this genetic knowledge??

    1. Well I assume that people working with java are used to that kind of notation. The package path is just javax.swing.table.DefaultTableModel as can be seen in the docs as well. But great that this post still gets attention

Comments are closed.