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 1005921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:22:27+00:00 2026-05-16T08:22:27+00:00

I have a list of Customer objects that I need to have selectable from

  • 0

I have a list of Customer objects that I need to have selectable from a JComboBox. From what I read I need to implement a custom renderer to have the fields I want displayed in the list.

I want my JComboBox to have entries formatted as such:

+----------------------------------------------+
|  Customer Name - Contact - City, State    V  |
+==============================================+
|  Customer #2 Name - Contact - City, State    |
|  Customer #3 Name - Contact - City, State    |
|  Customer #4 Name - Contact - City, State    |
|  Customer #5 Name - Contact - City, State    |
+----------------------------------------------+

I used this code:

public class CustomerListCellRenderer extends DefaultListCellRenderer {

@Override
public Component getListCellRendererComponent(
        JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
    if (value instanceof Customer) {
        Customer c = (Customer) value;

        StringBuffer sb = new StringBuffer();
        if (c.getCompany() != null && c.getCompany().length() > 0) {
            sb.append(c.getCompany());
        }
        sb.append(" - ");
        if (c.getCompany() != null && c.getCompany().length() > 0) {
            sb.append(c.getContact());
        }
        sb.append(" - ");
        if (c.getCompany() != null && c.getCompany().length() > 0) {
            sb.append(c.getCity());
            sb.append(", ");
        }            
        if (c.getCompany() != null && c.getCompany().length() > 0) {
            sb.append(c.getState());
        }

        setText(sb.toString());
    }
    return this;
  }
}

This doesn’t work correctly under Solaris / Unix / Linux using the system GTKLookAndFeel. The background of the input area of the combobox is not drawn and no border is drawn around it. (See screenshot below). Is there another way to achieve this that will work correctly across the 3 major platforms (Win/Mac/GTK)? Can I do a converter to do this and only manipulate the data not the GUI?

My current workaround is to override toString() on my Customer object to display each record in the format I want, but looking for other ideas.

alt text

Nick

  • 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-16T08:22:28+00:00Added an answer on May 16, 2026 at 8:22 am

    Same issue, I did this in order to customize it for showing icons:

    private static class CustomComboBoxRenderer extends DefaultListCellRenderer
    {
        private final ListCellRenderer backend;
    
        public CustomComboBoxRenderer(ListCellRenderer backend)
        {
            this.backend = backend;
        }
    
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
        {
            Component component = backend.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if(component instanceof JLabel == false)
                component = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            JLabel label = (JLabel)component;
            label.setIcon(Icons.COMPONENT);
            return label;
        }
    }
    

    Then assigned the renderer like this:

    comboBox.setRenderer(new CustomComboBoxRenderer(comboBox.getRenderer()));
    

    This has worked out fine for me, so far.

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

Sidebar

Related Questions

I have an app that has a list of items. My customer prefers to
i have list of rows that user select and i want to delete them,
I've got two lists of Customer objects, generated from Entity Framework. I need to
I have a list of Customers. Each customer has an address and some customers
I have a List of customers that I'm trying to bind to a DataGridView.
I have List I want to sort Desc by Priority, which is int and
I have Customer, Event and Address objects in my data model. Both Customer and
I have two ways that I am doing a fuzzy search for a customer.
I have a heterogeneous List that can contain any arbitrary type of object. I
Assume you have the following simple objects: class Order { public Customer[] Customers {

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.