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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:44:15+00:00 2026-06-17T15:44:15+00:00

This might be a dup – I can’t find it exactly though – I’m

  • 0

This might be a dup – I can’t find it exactly though – I’m basically simply trying to customize a JComboBox display by providing my own ListCellRenderer:

targetCombo = new JComboBox();
targetCombo .setRenderer(new BasicComboBoxRenderer(){
    public Component getListCellRendererComponent(JList list, Object value, int index,              boolean isSelected, boolean cellHasFocus){
        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        if (value != null){
            MyObj myObj = (myObj)value;
            setText(myObj.getName());
        }
        return this;
    }
});

The component properly displays the name when I expand the JComboBox list. However, on item selection, the display reverts to the toString() value of myObj.

Am I missing something?

  • 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-06-17T15:44:15+00:00Added an answer on June 17, 2026 at 3:44 pm

    Replace the use of BasicComboBoxRenderer with DefaultListCellRenderer

    public class TestComboBox04 {
    
      public static void main(String[] args) {
        new TestComboBox04();
      }
    
      public TestComboBox04() {
        EventQueue.invokeLater(new Runnable() {
          @Override
          public void run() {
            try {
              UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception ex) {
            }
    
            JComboBox targetCombo = new JComboBox();
            targetCombo.setRenderer(new DefaultListCellRenderer() {
              public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (value != null) {
                  MyObj myObj = (MyObj) value;
                  setText(myObj.getName());
                }
                return this;
              }
            });
            targetCombo.addItem(new MyObj("A"));
            targetCombo.addItem(new MyObj("B"));
            targetCombo.addItem(new MyObj("C"));
            targetCombo.addItem(new MyObj("D"));
    
            JFrame frame = new JFrame("Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new GridBagLayout());
            frame.add(targetCombo);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
    
          }
        });
      }
    
      public class MyObj {
    
        private String name;
    
        public MyObj(String name) {
          this.name = name;
        }
    
        public String getName() {
          return name;
        }
    
      }
    
    }
    

    You should never have a need to use the components from the look and feel packages, unless you are planning on creating your own look and feel.

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

Sidebar

Related Questions

This might be a simple question but I've searched and searched and can't find
This might be a silly question, but I can't seem to find a solution...
This might be a very basic question but I can't find the correct command
This might be a ridiculous question but I can't find it asked yet here
This might be a naive question. However, I am just trying to understand why
This might be a very basic question but it confuses me. Can two different
This might sound like a stupid question but just trying to learn something here.
This might be a easy question but i couldn't find solution. I want to
This might be a straightforward question but I can't see wood for trees at
This might be a pretty straightforward question, but I'm trying to understand some of

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.