Tuesday, 30 April 2013

How to use display member & value member in JComboBox in java


           Some times you need when you select anything from JComboBox you want to pass different information (like id of that think), so I will show you this :

Step 1) Create Model for JComboBox.


public class DisplayValueModel 
{
    public Object displayMember;
    public Object valueMember;
    
    public DisplayValueModel(Object display,Object value)
    {
        displayMember=display;
        valueMember=value;
    }
    
    public String toString()
    {
        return displayMember.toString();
    }
}

   This will hold both display member & value member for JComboBox...
   Here toString() method must be overriden, because it will decide what to display in JComboBox.

Step  2) Now in java file add directly the object of model to JComboBox

And that's it.....

Screenshot :



You can download the example :
Download

3 comments:

  1. Can you please share details!!!!!!

    ReplyDelete
  2. I have arraylist Object . then i add data in combobox. When i change item in combobox then it print two value ? How to fix

    ReplyDelete