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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:21:16+00:00 2026-05-14T19:21:16+00:00

I am populating a JComboBox (using addItem() ) with all the elements of a

  • 0

I am populating a JComboBox (using addItem()) with all the elements of a collection. Each element in the collection is a HashMap (so its a ComboBox of Hashmaps..).

My question is – Given that I need each item to be a HashMap how do I set the text to apear in the combobox on the GUI? It needs to be the value of a certain key in the map. Normally if I am populating a combobox with my own type, I would just overide the toString() method…but I am not sure how to acheive this since I am using a Java HashMap.

Any ideas (if possible, without implementing my own HashMap)?

Update: It seems like there isn’t anyway to avoid having the object int the JComboBox overide toString() if I want custom functionality..I wish there was a way to (1) specify the objects to be loaded into the JComboBox and (2) specify how these objects are to appear in the GUI.

  • 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-14T19:21:17+00:00Added an answer on May 14, 2026 at 7:21 pm

    (2) specify how these objects are to
    appear in the GUI.

    You can add any Object to the model and then create a custom renderer to display the object any way you want. Simple example that shows the toString() approach and custom renderer approach:

    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.plaf.basic.*;
    
    public class ComboBoxItem extends JFrame implements ActionListener
    {
        public ComboBoxItem()
        {
            Vector model = new Vector();
            model.addElement( new Item(1, "car" ) );
            model.addElement( new Item(2, "plane" ) );
            model.addElement( new Item(3, "train" ) );
            model.addElement( new Item(4, "boat" ) );
    
            JComboBox comboBox;
    
            //  Easiest approach is to just override toString() method
            //  of the Item class
    
            comboBox = new JComboBox( model );
            comboBox.setDragEnabled(true);
            comboBox.addActionListener( this );
            getContentPane().add(comboBox, BorderLayout.NORTH );
    
            //  Most flexible approach is to create a custom render
            //  to diplay the Item data
    
            comboBox = new JComboBox( model );
            comboBox.setDragEnabled(true);
            comboBox.setRenderer( new ItemRenderer() );
            comboBox.addActionListener( this );
            getContentPane().add(comboBox, BorderLayout.SOUTH );
        }
    
        public void actionPerformed(ActionEvent e)
        {
            JComboBox comboBox = (JComboBox)e.getSource();
            Item item = (Item)comboBox.getSelectedItem();
            System.out.println( item.getId() + " : " + item.getDescription() );
        }
    
        class ItemRenderer extends BasicComboBoxRenderer
        {
            public Component getListCellRendererComponent(
                JList list, Object value, int index,
                boolean isSelected, boolean cellHasFocus)
            {
                super.getListCellRendererComponent(list, value, index,
                    isSelected, cellHasFocus);
    
                if (value != null)
                {
                    Item item = (Item)value;
                    setText( item.getDescription().toUpperCase() );
                }
    
                if (index == -1)
                {
                    Item item = (Item)value;
                    setText( "" + item.getId() );
                }
    
    
                return this;
            }
        }
    
        class Item
        {
            private int id;
            private String description;
    
            public Item(int id, String description)
            {
                this.id = id;
                this.description = description;
            }
    
            public int getId()
            {
                return id;
            }
    
            public String getDescription()
            {
                return description;
            }
    
            public String toString()
            {
                return description;
            }
        }
    
        public static void main(String[] args)
        {
            JFrame frame = new ComboBoxItem();
            frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
            frame.pack();
            frame.setVisible( true );
         }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have an example of using of populating a YUI DataTable with a
I am using GridView in my application for populating datas. Is there any easy
I am currently building a .Net userform using C# and I am populating it
Initialization would include creating all the required tables, constraints and populating the tables. edit:
I am cloning a hidden table row then populating it and after validation I
I have a Drupal module page where I am populating a form with a
I would like to know what the best practice for populating a business object
I'm doing some Objective-C programming that involves parsing an NSXmlDocument and populating an objects
I did follow the article TRULLY Understanding ViewState (great article btw) and populating my
I am having a listbox in ASP.net. I am populating the listbox values from

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.