Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - How to make JComboBox of non-String objects display String names?

Imgur

I would like to make the JComboBox component display String names, rather than references. However, I don't know how that is done.

Below shows my codes:

public class Properties extends JPanel implements ItemListener {
    private static final long serialVersionUID = -8555733808183623384L;
    private static final Dimension SIZE = new Dimension(130, 80);
    private JComboBox<Category> tileCategory;

    public Properties() {
        tileCategory = new JComboBox<Category>();
        tileCategory.setPreferredSize(SIZE);
        tileCategory.addItemListener(this);

        this.setLayout(new GridLayout(16, 1));
        loadCategory();
    }

    private void loadCategory() {
        //Obtains a HashMap of Strings from somewhere else. All of this is constant, so they
        //aren't modified at runtime.
        HashMap<Integer, String> map = EditorConstants.getInstance().getCategoryList();

        DefaultComboBoxModel<Category> model = (DefaultComboBoxModel<Category>) this.tileCategory.getModel();
        for (int i = 0; i < map.size(); i++) {
            Category c = new Category();
            c.name = map.get(i + 1);
            model.addElement(c);
        }
        this.add(tileCategory);
    }
}

The only thing I know is that I passed Category class to JComboBox. Below shows the Category class:

public class Category {
    public String name;
}

And that's about it.

My only goal is to get the Category.name member variable show up in the JComboBox drop-down list, where the rectangle is marking in the picture.

Can anyone show me how this is done? Thanks in advance.

like image 996
tom_mai78101 Avatar asked Oct 21 '25 11:10

tom_mai78101


2 Answers

A JComboBox uses a ListCellRenderer to allow you to customise how the values are rendered.

Take a look at Providing a Custom Renderer for more details

For example...

public class CategoryListCellRenderer extends DefaultListCellRenderer {

    @Override
    public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {

        if (value instanceof Category) {
            value = ((Category)value).name;
        }

        return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); //To change body of generated methods, choose Tools | Templates.

    }

}

Then you simply specify the render to the combobox

tileCategory.setRenderer(new CategoryListCellRenderer());

Now, having said that, this will prevent the user from been able to use combo boxes built in search feature.

To that end, check Combo Box With Custom Renderer for a possible work around. This is authored by our very own camickr

like image 161
MadProgrammer Avatar answered Oct 24 '25 01:10

MadProgrammer


The easiest way would be to override the toString() method of your class. This is not a very robust solution, but gets the job done.

public class Category {
    public String name;

    @Override
    public String toString(){
        return name;
    }
}
like image 43
Balázs Édes Avatar answered Oct 24 '25 02:10

Balázs Édes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!