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

  • Home
  • SEARCH
  • 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 560101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:18:27+00:00 2026-05-13T12:18:27+00:00

I have a model class that stores keys and values: public class KeyValue {

  • 0

I have a model class that stores keys and values:

public class KeyValue {

    private Object key;
    private String value;

    KeyValue () {
    }

    KeyValue (Object key, String value) {
        this.key=key;
        this.value=value;
    }

    public Object getKey() {
        return this.key;
    }
    public void setKey(Object key) {
        this.key=key;
    }

    public String getValue() {
        return this.value;
    }
    public void setValue(String value) {
        this.value=value;
    }

    @Override
    public String toString() {
        return this.value;
    }

}

I use this class to populate a JComboBox‘s Model:

for (int i = 0; i < universes.length; i++) {
    ComboBox_Universes.addItem(new KeyValue(infoObject.ID,infoObject.title));
}

I would like to refactor this logic to use a Java collection class (call it KeyValueCollection) that can support two objectives:

1) the KeyValueCollection can be used to populate the JComboBox‘s Model. Something like:

//get a KeyValueCollection filled with data from helper class
KeyValueCollection universeCollection = Repository.getUniverseCollection();

//use this collection as the JComboBox's model
ComboBox_Universes.setModel(universeCollection);

2) I can use the KeyValueCollection to convert a key to a value:

//ID retrieve from another control
int universeID = (int)this.Table_Values.getModel().getValueAt(row, COLUMN_ID);

//convert ID to name
String universeName = universeCollection.get(universeID).getValue();

In the .NET world, I would use the KeyedCollection class for this, but I’m not very familiar with Java.

Help is greatly appreciated.

  • 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-13T12:18:28+00:00Added an answer on May 13, 2026 at 12:18 pm

    You can use a custom class like this one (run main function to see its behavior) :

    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    import java.util.TreeMap;
    
    import javax.swing.AbstractListModel;
    import javax.swing.ComboBoxModel;
    import javax.swing.DefaultListCellRenderer;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JPanel;
    
    public class KeyValueComboboxModel extends AbstractListModel implements ComboBoxModel, Map<String, String> {
    
        private TreeMap<String,String> values = new TreeMap<String,String>();
    
        private Map.Entry<String, String> selectedItem = null;
    
        public Object getSelectedItem() {
            return selectedItem;
        }
    
        public void setSelectedItem(Object anItem) {
            this.selectedItem = (java.util.Map.Entry<String, String>) anItem;
            fireContentsChanged(this, -1, -1);
        }
    
        public Object getElementAt(int index) {
            List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(values.entrySet());
            return list.get(index);
        }
    
    
    
        public int getSize() {
            return values.size();
        }
    
        public void clear() {
            values.clear();
        }
    
        public boolean containsKey(Object key) {
            return values.containsKey(key);
        }
    
        public boolean containsValue(Object value) {
            return values.containsValue(value);
        }
    
        public Set<java.util.Map.Entry<String, String>> entrySet() {
            return values.entrySet();
        }
    
        public String get(Object key) {
            return values.get(key);
        }
    
        public Set<String> keySet() {
            return values.keySet();
        }
    
        public String put(String key, String value) {
            return values.put(key, value);
        }
    
        public String remove(Object key) {
            return values.remove(key);
        }
    
        public int size() {
            return values.size();
        }
    
        public Collection<String> values() {
            return values.values();
        }
    
        public boolean isEmpty() {
            return values.isEmpty();
        }
    
        public void putAll(Map<? extends String, ? extends String> m) {
            values.putAll(m);
        }
    
    
        private static String entryToString(Map.Entry<String, String> entry) {
            String str = "" + entry.getKey() + "->" + entry.getValue();
            return str;
        }
    
        public static void main(String[] args) {
    
            Map<String,String> map= new HashMap<String,String>(){{
                put("1","blue");
                put("2","red");
                put("3","white");
                put("4","black");
            }};
    
            JFrame f = new JFrame();
            f.setContentPane(new JPanel(new BorderLayout()));
    
            KeyValueComboboxModel model = new KeyValueComboboxModel();
            model.putAll(map);
    
            final JComboBox combo = new JComboBox(model);
            combo.setRenderer(new DefaultListCellRenderer(){
    
                @Override
                public Component getListCellRendererComponent(JList list, Object value, int index,
                        boolean isSelected, boolean cellHasFocus) {
                    if(value instanceof Map.Entry){
                        Map.Entry<String,String> entry = (java.util.Map.Entry<String, String>) value;
                        String str = entryToString(entry);
                        return super.getListCellRendererComponent(list, str, index, isSelected, cellHasFocus);
                    }
                    return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                }
    
            });
    
            final JLabel lab = new JLabel("Nothing selected");
    
            combo.addActionListener(new ActionListener(){
    
                public void actionPerformed(ActionEvent e) {
                    if(combo.getSelectedItem()!=null){
                        lab.setText(entryToString((java.util.Map.Entry<String, String>) combo.getSelectedItem()));
                    } else {
                        lab.setText("");
                    }
    
                }
    
            });
    
            f.getContentPane().add(combo,BorderLayout.CENTER);
            f.getContentPane().add(lab,BorderLayout.SOUTH);
    
            f.setSize(300,80);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setLocationRelativeTo(null);
            f.setVisible(true);
    
    
        }
    
    
    }
    

    EDIT : to handle the selected item and keys, you may add these methods:

    public void setSelectedKey(String key){
        selectedItem = values.ceilingEntry(key);
        setSelectedItem(key);
    }
    
    public void setSelectedItem(String key, String value){
        values.put(key, value);
        setSelectedKey(key);
    }
    

    By default, values are ordered following the natural order of the keys (alphabetical order of the keys, here, because these are String). If you need an other ordering, add a java.util.Comparator to the TreeMap (see TreeMap documentation).

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

Sidebar

Ask A Question

Stats

  • Questions 429k
  • Answers 429k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Because it forces anyone who uses your header file to… May 15, 2026 at 1:27 pm
  • Editorial Team
    Editorial Team added an answer I imagine a simple way would be to do: myRtb.Text… May 15, 2026 at 1:27 pm
  • Editorial Team
    Editorial Team added an answer Path may be the important bit of that error message… May 15, 2026 at 1:27 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.