Java:Set Checkbox selected according to the value of database

Sometimes we may need to set the checkbox selected according to the value in database.

In our example  buyer_name,item_number, order_number, and style_number are the database field and  their value  in database is set to True or False according to software  requirement

Sample Database :

Config_parameter (varchar) Config_value (varchar)
buyer_name True
item_number False
order_number True
style_number True
user_name admin
password xyz

 

Here is the sample code 

                        Vector heading=new Vector();

                        heading.add(” About”);

                        heading.add(“Value”);

                        Vector config=new Vector();

                        Vector data=new Vector();

                        try {

         config=Inter.getDetails();  //  function Inter.getDetails()  Collects data from database

                         data=(Vector)config.get(0);

                        } catch (RemoteException e) {   

                                    e.printStackTrace();

                        }

                        jtableView.setModel(new MyModel(data,heading));

                         jtableView.repaint();

HashMap hm=(HashMap)config.get(1);

                if(hm.get(“buyer_name”)!=null)   

                {

 // if database field (buyer_name)= =true  then chkbox_buyer_name   will be  setSelected

 chkbox_buyer_name.setSelected((hm.get(“buyer_name”).toString().equalsIgnoreCase(“true”) ? true : false));

                }

                if(hm.get(“item_number”)!=null)

                {

// if database field (item_number)= =true  then chkbox_item_number   will be  setSelected

chkbox_item_number.setSelected((hm.get(“item_number”).toString().equalsIgnoreCase(“true”) ? true : false));

                }

                if(hm.get(“order_number”)!=null)

                {

// if database field (order_number)= =true  then chkbox_order_number will be  setSelected

chkbox_order_number.setSelected((hm.get(“order_number”).toString().equalsIgnoreCase(“true”) ? true : false));

                }

                if(hm.get(“style_number”)!=null)

                {

chkbox_style_number.setSelected((hm.get(“style_number”).toString().equalsIgnoreCase(“true”) ? true : false));

                }

                if(hm.get(“style_description”)!=null)

                {

                chkbox_style_description.setSelected((hm.get(“style_description”).toString().equalsIgnoreCase(“true”) ? true : false));

                }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: