Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6977483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:37:53+00:00 2026-05-27T17:37:53+00:00

I am trying to set up a combo box that will display a UserName,

  • 0

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.

Thanks,

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 {
INSTANCE;

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.setInput(ModelProvider.INSTANCE.getAnimals());
            comboViewer_9.setLabelProvider(new LabelProvider() {
                  @Override
                  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);
        }
        results.close();
        stmt.close();
        CC_Derby.shutdown(stmt, conn);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:37:54+00:00Added an answer on May 27, 2026 at 5:37 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to set a TwoWay binding to a combo box using only a
I am trying to set the selected value of a combobox from a style
I'm trying to set the selected value of my Font Family combobox, which has
I was trying to create a custom combobox that inherited from ComboBox, but I've
I have: ComboBox.DataSource = System.Enum.GetValues(typeof(ImageLayout)); but how do I set the ComboBox.SelectedItem to an
I've got a Combo Box that is databound to an ObservableCollection of items. I
I'm trying to understand how validation works for a combo box when its ItemsSource
I have a combo box and several text boxes on a form. When I
I have a combo box on a WinForms app in which an item may
I have a WPF CustomControl that is derived from ComboBox and I'm trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.