Java-如何使非字符串对象的JComboBox显示字符串名称? [英] Java - How to make JComboBox of non-String objects display String names?

查看:73
本文介绍了Java-如何使非字符串对象的JComboBox显示字符串名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让JComboBox组件显示String名称,而不是引用.但是,我不知道该怎么做.

下面显示了我的代码:

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);
    }
}

我唯一知道的是我将Category类传递给了JComboBox.下面显示了Category类:

public class Category {
    public String name;
}

就是这样.

我唯一的目标是使Category.name成员变量显示在JComboBox下拉列表中,该矩形标记在图片中.

谁能告诉我这是怎么做的?预先感谢.

解决方案

JComboBox使用ListCellRenderer允许您自定义值的呈现方式.

请参见提供自定义渲染器以了解更多详细信息

例如...

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.

    }

}

然后您只需将渲染指定到组合框

tileCategory.setRenderer(new CategoryListCellRenderer());

现在,话虽如此,这将阻止用户使用内置于搜索功能的组合框.

为此,请检查带有自定义渲染器的组合框可能的解决方法.这是由我们自己的camickr创作的

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.

解决方案

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

这篇关于Java-如何使非字符串对象的JComboBox显示字符串名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆