Blog

What is a Custom Data Model & how to use it

To be able to understand this tutorial, you should know how to use any of these components - jComboBox or jList or jTable. Reference database – Our good old college information system GUI components - Swing, IDE - NetBeans (6.8.1 or higher). Many of you might not have even heard about data models. Let me assure you, they very much exist & are used widely. Even you can use them, without waiting to work on big projects. You add this learning to your small academic projects & make them big instead. Let us understand a data model through a sequence of steps.

  • Add a Combo Box - Add any combo box onto a JPanel. Let's name it stdtCombo. By default, the data that you see in the stdtCombo is a list of items named ‘Item1’, ‘Item2’, etc.
  • Change the Data Model - Right click on the stdtCombo to navigate to its properties. You will find that the property field ‘model’ is the source of this data. This is nothing but your data model. Because this is the default model for the jCombBoxit is named 'DefaultComboBoxModel'.
  • Normally, if you want to replace this data with your own, say names, you would start replacing ‘Item1’ by ‘Akash’, ‘Item2’ by ‘Amit’ & so on,  in this field.     

But this model is static i.e. it will ALWAYS show these names; the data cannot be changed on any specific trigger or action at runtime. So, is there a way to create a dynamic custom data model? One that would show, say names of boys – ‘Akash', 'Amit', 'Anand’ on the click of a jButton1 labeled ‘Show boys in class’? Or show names of girls – ‘Raina', 'Reema', 'Riddhi’ on  click of another jButton2 labeled ‘Show girls in class’? Yes, there is. We are assuming that our database has various details of the student stored - name, gender, etc. & we can come to know the student’s gender & name by searching on the keystudentRollNo in tblStudent.

  • Get the data you want to be populated - Let’s assume we run a database query & get all students of ‘male’ gender from tblStudent. Just to illustrate, in MySQL it would be something like - SELECT studentName FROM tblStudent WHERE studentGender = 'MALE'; (Assuming studentGender is of typevarchar).            
  • Add the data to an array list - We add each of these names obtained in the resultset to an ArrayList object named boysList and return it to the Swing jPanel form.

A quick recap, before we move to the actual implementation. So, the jComboBox (& similar components) has some data model attached to it by default, called the DefaultComboBoxModel. But it is static, meaning the data model is not separated from the view. Every time I will use the combo it will have ONLY THIS data to show. But in any application, user might want that things be dynamic, i.e. the combo displays data given by the user at runtime. Whenever we, as users, want to customize existing functionality, we override default behaviour. So, we will override the DefaultComboBoxModel with our customized data model which we call the 'Custom Data Model'. All said, how do we do it.

  • Create an event - Create an event on button click for jButton1. Add the below code in the event handler.
  • Instantiate the model - Instanatiate the DefaultComboBoxModel as -

             DefaultComboBoxModel cbModel = new DefaultComboBoxModel(); (Do not forget to add import javax.swing.DefaultComboBoxModel)

  • Add each element of the arraylist to the model - Add each name in the boysList to cbModel through a loop.  (Refer APIs of ArrayList & DefaultComboBoxModel)

             for (int pos = 0; pos < boysList.size(); pos++) {                cbModel.addElement(boysList.get(pos));          }

  • Set the model in the combo box - Once you have added all elements, set this model for the stdtCombo -

             stdtCombo.setModel(cbModel );

Now, you have a 'Custom Data Model', which will show the names of all boys in the class on the click of jButton1. Similarly, you can display names of girls on click of jButton2.

Now, for some formal definitions -

What is a custom data model? A 'Custom Data Model' is a data model customized by the user. This is usually created to separate data from view i.e. not attaching ANY SPECIFIC data to the view component (the stdtCombo here). This is done by overriding the default data model with custom or user defined data model (in our case the cbModel). The user will decide with what data the combo should be populated. The source of this data can be an array, arraylist or any other structure. The 'Custom Data Model' will be populated with different data depending on user action/triggers, etc. so that on different triggers, the view component displays different data.

'Custom Data Model' offers flexibility of use. It definitely is a good thing to learn since real world applications seldom deal with static data. It might seem too much at the start, as with any concept in Java :). But do persist, if you are on the way to learning the REAL Java. And, use this tutorial to get that initial thrust!

Contact Us

R-58, Zone 1 MP Nagar, Bhopal - 462011

Call - +91 755 407 6667

E-mail - contactus@genpro.co.in