JavaFX 可编辑组合框:在项目选择时显示 toString [英] JavaFX Editable ComboBox : Showing toString on item selection

查看:21
本文介绍了JavaFX 可编辑组合框:在项目选择时显示 toString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ComboBox,类型为 Person,其中我添加了几个 Person 类的对象.

I have a ComboBox<Person>, of type Person, in which I have added few object of Person class.

我已经使用 setCellFactory(Callback) 方法在 ComboBox 下拉菜单中显示 Person 名称

I have used setCellFactory(Callback) method to show Person name in ComboBox drop down

combobox.setCellFactory(
    new Callback<ListView<Person >, ListCell<Person >>() {
        @Override
        public ListCell<Person > call(ListView<Person > p) {
            ListCell cell = new ListCell<Person >() {
                @Override
                protected void updateItem(Person item, boolean empty) {
                    super.updateItem(item, empty);
                    if (empty) {
                        setText("");
                    } else {
                        setText(item.getName());
                    }
                }
            };
            return cell;
        }
    });

并且,setButtonCell(ListCell) 方法在选择时在 combobox 中显示名称.

And, setButtonCell(ListCell) method to show name in combobox on selection.

combobox.setButtonCell(
    new ListCell<Object>() {
        @Override
        protected void updateItem(Person t, boolean bln) {
            super.updateItem(t, bln); 
            if (bln) {
                setText("");
            } else {
                setText(t.getName());
            }
        }
    });

这在正常情况下效果很好,但是当我使用可编辑的 combobox 时,这会失败.

This works perfectly good with normal case but when I use editable combobox then this fails.

当我写时,combobox.setEditable(true); 然后在项目选择时 combobox 的文本字段(编辑器)显示 toString() Person 类的方法.

When I write , combobox.setEditable(true); then on item selection the text field (editor) of combobox shows toString() method of Person class.

正常情况:

可编辑案例:

有什么解决办法吗?

我有一个模型类:

public class Person {
    String name;
    int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" + "name=" + name + ", age=" + age + '}';
    }
}

推荐答案

这是我自己的问题的答案,经过多次努力和更正,我发现这个答案是最好的.

Here is an answer to my own question which I found best after many efforts and corrections.

mainComboBox.setButtonCell(
    new ListCell<Object>() {
        @Override
        protected void updateItem(Object t, boolean bln) {
            super.updateItem(t, bln);
            if (bln) {
                setText("");
            } else {
                setText(getStringField(t));
            }
        }
    });

mainComboBox.setConverter(
    new StringConverter() {
        private Map<String, Object> map = new HashMap<>();

        @Override
        public String toString(Object t) {
            if (t != null) {
                String str = getStringField(t);
                map.put(str, t);
                return str;
            } else {
                return "";
            }
        }

        @Override
        public Object fromString(String string) {
            if (validate && !map.containsKey(string)) {
                mainComboBox.setValue(null);
                mainComboBox.getEditor().clear();
                return null;
            }
            return map.get(string);
        }
    });

mainComboBox.setCellFactory(
    new Callback<ListView<Object>, ListCell<Object>>() {
        @Override
        public ListCell<Object> call(ListView<Object> p) {
            ListCell cell = new ListCell<Object>() {
                @Override
                protected void updateItem(Object item, boolean empty) {
                    super.updateItem(item, empty);
                    if (empty) {
                        setText("");
                    } else {
                        setText(getStringField(item));
                    }
                }
            };return cell;
        }
    });

并带有getStringField(Object)的必需函数,

public String getStringField(Object o) {
    return ((Pesron) o).getName();
}

这篇关于JavaFX 可编辑组合框:在项目选择时显示 toString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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