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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:22:58+00:00 2026-05-13T18:22:58+00:00

Is there a comparable mechanism to the .NET set DisplayMember for the JListBox Swing

  • 0

Is there a comparable mechanism to the .NET “set DisplayMember” for the JListBox Swing component?

Overriding the toString() method is not sufficient because I also want to be able to change the display member at runtime.

I also looked into implementing my own ListCellRenderer, but found it not very convenient.

Ist there a easier or more elegant approach?

Thanks for your time.

  • 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-13T18:22:58+00:00Added an answer on May 13, 2026 at 6:22 pm

    I came up with a satisfying solution based on implementing a custom ListCellRenderer.

    import java.awt.Component;
    import javax.swing.DefaultListCellRenderer;
    import javax.swing.JList;
    import javax.swing.ListCellRenderer;
    
    public class DynamicCellRenderer implements ListCellRenderer {
        private final ListCellRenderer listCellRenderer;
        private String displayMember;
    
        public DynamicCellRenderer(String displayMember) {
            this(displayMember, new DefaultListCellRenderer());
        }
    
        public DynamicCellRenderer(String displayMember, ListCellRenderer wrapped) {
            listCellRenderer = wrapped;
            this.displayMember = displayMember;
        }
    
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            Object displayMemberValue = getDisplayMemberValue(value);
            return listCellRenderer.getListCellRendererComponent(list, displayMemberValue, index, isSelected, cellHasFocus);
        }
    
        private Object getDisplayMemberValue(Object value) {
            // value is the domain class
            // only works if display member is a method, TODO: fallback to field
            // displayMember is something like "getName" -> value.getName() gets called
            try {
                return value.getClass().getMethod(displayMember).invoke(value);
            } catch (Exception ex) {
                // if anything went wrong it is the programmers fault -> propagate exception
                throw new RuntimeException(ex);
            }
        }
    
        public String getDisplayMember() {
            return displayMember;
        }
    
        public void setDisplayMember(String displayMember) {
            this.displayMember = displayMember;
        }
    
    }
    

    In your client GUI code you can do something like this:

    jListBox1.setCellRenderer(new DynamicCellRenderer("getName"));
    ...
    ...
    // and later at some point
    ((DynamicCellRenderer) jListBox1.getCellRenderer()).setDisplayMember("getEmail");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any comparable function like Pos that is not case-sensitive in D2010 (unicode)?
This question is related to a previous post . Is there something comparable to
there is an immutable class: Scope<Cmp extends Comparable<Cmp>> public Scope<Cmp> crop(Scope<Cmp> scope) { ...
Is the ASP.NET 4.0 SQL session state mechanism backward-compatible with the ASP.NET 2.0 schema
Is there something comparable to explicit interface implementation but for classes instead in C#?
First thing: I know that there is interface to W3C validator: http://pear.php.net/package/Services_W3C_HTMLValidator/ But I
I was bummed out to see that scikit-learn does not support Python 3...Is there
Is Berkley Packet Filter supported on Windows or is there something comparable which I
There was method setWrappedInstance in org.springframework.beans.BeanWrapper in 2.5.6 and was removed in 3.0.0. As
Is there a way to detect database events, e.g. insert, update and delete, comparable

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.