I want to to do the following but can't figure out how to do it in Java.

I retrieve information, including the primary key, from a database. Using this information, I load a comboBox. The user selects an item from the comboBox. Using that selection, I build an SQL join to retrieve additional information.

My Problem: I can't figure out how to keep track of the relationship between the items in the comboBox and the primary key.

Appreciate some help..


8 Years
Discussion Span
Last Post by bruceaj


When you add an Object to the Combo Box, the toString() method is called and it displays that at the GUI: void addItem(Object anObject) The method Object getSelectedItem() gets you the item you have selected.

So create an object with all the info from the DB including the primary key:

class Data {
  private long id = 0L; // assuming this is the PK
  private Sting name = null;
  private int age = 0;

  public String toString() {
      return name;

Get the data from the DB, put each row in a Data object and put that object into a List (java.util.Vector).

Then add each of the objects from the List to Combo Box


Thank you very much for the response. Hopefully, I'll get a chance to try it today. I'll get back when I do. Thanks again.


I'm sorry. I guess I didn't completely understand what you told me. I use the excuse that I'm a newbie!! Nothing is displayed in the comboBox. It appears it has the correct number of items but the dropdown box is empty. Here's my code. Appreciate your comments.

/* This class holds the contents of the database ResultSet */
public class Data {

    private String author = null;     //  Author's name
    private long primaryKey = 0L;   // Primary Key

    public void setName(String n){
        author = n;

    public void setID(long id) {
        primaryKey = id;
    // When you add an Object to the Combo Box,
    // the toString() method is called and it displays that at the GUI:

    public String toString() {
        return author;

Data data = null;   // Create an object to hold the database information
        Vector dataList = new Vector();

        // Retrive a complete list of authors from the database
        // formatted last, first.

        resultSet = dbManage.Query(SELECT);       // Get the data from the database

        try {
            while (resultSet.next()) {
                data = new Data();          // Create a Data object

                data.setName(resultSet.getString("Name"));  // Save the author's name
                data.setID(resultSet.getLong("authorID"));  // Save the author ID

                dataList.add(data);     // add the data row to the list
        } catch (SQLException e) {

        for (int i = 0; i < dataList.size(); i++) {

            cboAuthors.addItem(dataList.get(i));    // add the list to the comboBox

Edited by bruceaj: Techical errors.


The code in the previous post is okay. It was a user error on my part and I didn't catch it until my "edit post" time had expired. Sorry...

I still haven't tested retrieving the selected item. I'll get back when I have done that. Sorry for the bad post. And sorry I didn't catch until after the 30 minute edit time limit.


I retrieved the selected item into a "Data" object, which is the class I created to addItem the data. And there the row data was. Thank you.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.