JavaFX无法通过自定义类选择ComboBox

我正试图给我的订单一个新客户,所以我创建了一个ComboBox,其中有我可以选择的所有客户:

//I chose to use Customer instead of String so I can have duplicates and save the right one later
@FXML private ComboBox<Customer> selectionBox;

public void initialize() {
    // Adding all customers here,simplified example:
    int id = 1;
    String name = "Grace";
    Customer customer = new Customer(id,name);
    selectionBox.getItems().add(customer);
}

这是我的客户类的简化:

public class Customer {
    private int id;
    private int name;

    public Customer(int id,String name) {
        this.id = id;
        this.name = name;
    }

    public String toString() {
        // This is here to show the name in the ComboBox instead of 'Customer.java@37hf'
        return name;
    }
}

我的Order类:

public class Order {
    private int id;
    private Customer customer;    

    public Order(int id,Customer customer) {
        this.id = id;
        this.customer = customer
    }

    public void saveSql(Customer newCustomer) {
        this.customer = newCustomer;
        // Sql code here
    }
}

只要我想与新客户更新订单并执行
order.saveSql(selectionBox.getSelectionmodel().getSelectedItem())
出现此错误:
Exception in thread "JavaFX Application Thread" java.lang.ClassCastException: class java.lang.String cannot be cast to class my.package.Customer
这说明selectionBox.getSelectionmodel().getSelectedItem()是一个字符串,而日食说它是一个“客户”,并且它应该是客户,因为我使用ComboBox<Customer>

初始化了ComboBox

对此我该怎么办?
感谢您的帮助!

iCMS 回答:JavaFX无法通过自定义类选择ComboBox

您收到String错误,因为当ComboBox可编辑时,您正在编写的内容是TextField,它返回了String。当您从该字段“失去焦点”(例如,按Alt + Tab,单击其他字段等)或按“ Enter”时,系统尝试将String添加到ComboBox列表,但是由于上述ComboBox的类型为Customer,因此不能。

要管理该问题,您需要使用StringConverter。有关实现方法的信息,请参见this

编辑

摆弄后,我发现了this。 我已设法使其适应您的情况。

package sample;

public class Customer {
    private int id;
    private String name;

    public Customer(int id,String name) {
        this.id = id;
        this.name = name;
    }

    // Getters & Setters
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    // You don't need to override the toString() method for this
}

CustomerConverter自定义 Converter类。

package sample;

import javafx.util.StringConverter;

public class CustomerConverter extends StringConverter<Customer> {

    private int customerId;

    // Method to convert a Customer-Object to a String
    @Override
    public String toString(Customer customer)
    {
        return customer == null? null : customer.getName();
    }

    // Method to convert a String to a Customer-Object
    @Override
    public Customer fromString(String string) {

        Customer customer = null;

        if (string == null) {
            return customer;
        }

        customer = new Customer(customerId,string);

        return customer;
    }

    public void setCustomerId(int customerId) {
        this.customerId = customerId;
    }

}

Controller的{​​{1}}类。

FXML
本文链接:https://www.f2er.com/2276850.html

大家都在问