tags:

views:

41

answers:

1

Hi,

i am .net c# programmer that wanna learn Java these days.I can connect DB , getting and writing data with JDBC.But How can i fill JComboBox and set its DisplayMember "PersonelName" and ValueMember "PersonelID". In .NET there is related properties like DisplayMember,DataSource,ValueMember , i can show personel's name with displaymember and when i writing to data i can learn personel's id from valuemember property.But in Java there is no properties like this. How can i get PersonelID and show Personel Name in JCombobox in Java ? Can someone gimme an example. thanx.

+2  A: 

JComboBox constructor can take a ComboBoxModel as argument. DefaultComboBoxModel is a concrete implementation of the ComboBoxModel interface.

So, if you have a Personel class:

class Personel{
    String personelName;
    int personelId;
    //getters, setters

    //This will be your display member
    @Override
    public String toString(){
        return this.personelName;
    }
}

And supposing you obtained all personel via JDBC and have it stored in a new Vector, you can do:

DefaultComboBoxModel comboModel = new DefaultComboBoxModel(personel);
JComboBox myCombo = new JComboBox(comboModel);

At runtime, you can getModel and setModel to access the JComboBox model. The display member will be Personel's toString() method. The value member will be the actual object stored in the DefaultComboBoxModel vector, in this case a Personel instance.

I suggest you take a look at the API documentation for ComboBoxModel and DefaultComboBoxModel.

Cesar