Take the 2-minute tour ×

I am trying to set up a combo box that will display a UserName, but when selected will send a an ID instead.

I have already set up a method that queries my Derby database and returns a multidimensional array with the UserNames and IDs.

I did some research on how to set this up and found several posts suggesting JFace, but nothing on how to accomplish this.

I have tried setData(String Key, Object Value), and add(String String, int index). I have also looked at DataBinding, but haven't found anything that I am able to make work.


Update---------------------- I got it working to a point. I think I have the content provider set up properly however when I go to run the app it prints all the labels the same as the last item in the list.

Model Provider:

public enum ModelProvider {

public List<Animal> Animals;
public String conn = "L:\\Dropbox\\Herd_Management\\database";
private ModelProvider() {
    Animals = new ArrayList<Animal>();
    String strSQL = null;
    strSQL = "SELECT Herd_ID FROM dbherd_management.TBLHERD";
    String[] straHerd_IDs = CC_Derby.getResultArray(conn, "", "", strSQL);
    for (int r = 0; r < straHerd_IDs.length; r++) {
        Animals.add(new Animal(Integer.parseInt(straHerd_IDs[r])));

public List<Animal> getAnimals() {
    return Animals;


Snippet For Content and Label Providers:

            comboViewer_9 = new ComboViewer(shlHerdManagement, SWT.NONE);
            final Combo cboAnimalTag = comboViewer_9.getCombo();
            cboAnimalTag.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
                    true, false, 1, 1));
            cboAnimalTag.setText("<Select Animal Tag>");

            comboViewer_9.setContentProvider(new ArrayContentProvider());
            comboViewer_9.setLabelProvider(new LabelProvider() {
                  public String getText(Object element) {
                      Animal a = (Animal) element;
                      return (a.getTag());

Animal Constructor

public Animal(int intAnimal_ID) {
    Connection conn = CC_Derby.createConnection("L:\\Dropbox\\Herd_Management\\database","","");
    String strSQL = "SELECT * FROM dbherd_management.TBLHERD WHERE HERD_ID ="
            + intAnimal_ID;

    try {
        stmt = conn.createStatement();
        ResultSet results = null;
        results = stmt.executeQuery(strSQL);
        while (results.next()) {
            intHerd_id = results.getInt(1);
            strHerd_Tag_Letter = results.getString(2);
            intHerd_Tag_Num = results.getInt(3);
            strHerd_Tag_Color = results.getString(4);
            strHerd_Sex = results.getString(5);
            strHerd_Type = results.getString(6);
            dtHerd_Birthdate = results.getString(7);
            intHerd_Sire = results.getInt(8);
            intHerd_Dam = results.getInt(9);
            intHerd_Owner = results.getInt(10);
            strHerd_TimeStamp = results.getString(11);
            strHerd_Status = results.getString(12);
            dtHerd_Status_Date = results.getString(13);
        CC_Derby.shutdown(stmt, conn);
    } catch (SQLException e) {
        // TODO Auto-generated catch block


getTag Method

public String getTag(){
    String strResult = null;
    String l = null;
    int n = 0;
    String c = null;
    l = getHerd_Tag_Letter();
    n = getHerd_Tag_Num();
    c = getHerd_Tag_Color();
    if(l == null){
        l ="";
    if(c == null){
        c = "";
    strResult = l + n + " " + c;
    return strResult;
share|improve this question
Viewer setup looks OK. It's not clear from the code what is the value of getTag(). –  Martti Käärik Dec 22 '11 at 6:59
I added the Animal Constructor and getTag() Method –  Talon06 Dec 22 '11 at 12:17
I don't see any problems here, sorry. I can only suggest debugging... –  Martti Käärik Dec 22 '11 at 12:30
Ok one more question. How do I go about getting the key value stored in the combo? I'm hoping if I can get the key values it will help me see what is going on with the labels. –  Talon06 Dec 22 '11 at 14:23
Add ISelectionChangedListener to the viewer. –  Martti Käärik Dec 22 '11 at 22:10
add comment

1 Answer

up vote 0 down vote accepted

You have a viewer with a content provider and a label provider. Content provider serves the objects (UserNameIdPair), label provider provides labels (UserName string). View's selection listeners will be "sent" objects provided by content provider.

share|improve this answer
Edit Question with new findings. –  Talon06 Dec 22 '11 at 2:47
add comment

Your Answer


