I'm not sure if this is the best way but I would like to display a list of addresses in a combo box where the columns come from different tables such as Countries, Cities and Types (work and home).

I don't want to display the countryID or cityID, etc. but for know I have simply selected all from the address table to start getting this to work.

Problem: From using the code below, only the first column appears.

if(vehicleAccessType == 1) {
getDBConnection();
try {
stmt = conn.createStatement();
if(stmt.execute("SELECT * FROM Collection_Addresses.CollectionAddressID, Collection_Addresses.AddressLine, Collection_Addresses.AddressLine2, Collection_Addresses.Postcode FROM Collection_Addresses, Clients WHERE Collection_Addresses.CollectionAddressID = Clients.CollectionAddressID")) {
rs = stmt.getResultSet();
}
while(rs.next()) {
cbCustomerAddresses.addItem(new AddressComboBoxItem(rs.getInt(1), rs.getString(2).trim(), rs.getString(3).trim(), rs.getString(4).trim());
}
}
catch (SQLException ex) {
	System.out.println("SQLException: " + ex.getMessage());
	return;
	}
}

Dani AI

Generated

Two things to check first: the SQL and how the JComboBox renders objects. As pointed out, a JComboBox shows whatever the item's toString() returns (unless you set a custom renderer). Also, the query in the original snippet is fragile — list the columns after SELECT and use explicit JOINs (or aliases) so you get city/country/type names instead of numeric IDs. Calling trim() on rs.getString(...) can NPE if the column is NULL; guard that.

A simple and safe fix is to keep the item object (so the ID stays with the item) and override toString() to return the human-friendly address. Example Address item that trims safely and builds a readable address:

public class AddressComboBoxItem {
    private final int id;
    private final String line1, line2, city, postcode;

    public AddressComboBoxItem(int id, String line1, String line2, String city, String postcode) {
        this.id = id;
        this.line1 = safe(line1);
        this.line2 = safe(line2);
        this.city = safe(city);
        this.postcode = safe(postcode);
    }

    private static String safe(String s) { return s == null ? "" : s.trim(); }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(line1);
        if (!line2.isEmpty()) sb.append(", ").append(line2);
        if (!city.isEmpty()) sb.append(", ").append(city);
        if (!postcode.isEmpty()) sb.append(" ").append(postcode);
        return sb.toString();
    }

    public int getId() { return id; }
}

If you need richer display (multi-line, different fonts), set a ListCellRenderer on the combo. For the SQL side, use explicit joins and optionally build a FullAddress in the query (SQL Server example):

SELECT a.CollectionAddressID, a.AddressLine, a.AddressLine2, c.CityName, co.CountryName, a.Postcode
FROM Collection_Addresses a
JOIN Cities c ON a.CityID = c.CityID
JOIN Countries co ON a.CountryID = co.CountryID

Further reading: Swing JComboBox usage and renderer patterns are documented in the Swing tutorial and the JComboBox Javadoc: Swing JComboBox Tutorial and JComboBox Javadoc.

You didn't post the AddressComboBoxItem class, so no one can see what that does.
I can tell you that the combo box is going to display whatever your item's toString() method returns, unless you have created a custom renderer for it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.