JavaFX可以';无法使用自定义类获取组合框的选择
我正在尝试为我的订单提供一个新客户,因此我创建了一个组合框,其中包含我可以从中选择的所有客户:JavaFX可以';无法使用自定义类获取组合框的选择,java,javafx,combobox,fxml,Java,Javafx,Combobox,Fxml,我正在尝试为我的订单提供一个新客户,因此我创建了一个组合框,其中包含我可以从中选择的所有客户: //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 custo
//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 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())
它给出了这个错误:
线程“JavaFX应用程序线程”java.lang.ClassCastException中的异常:无法将class java.lang.String转换为class my.package.Customer
这说明
selectionBox.getSelectionModel().getSelectedItem()
是一个字符串,而eclipse说它是一个“客户”,应该是一个客户,因为我使用ComboBox
初始化了ComboBox
对此我能做些什么?谢谢你的帮助 您会得到
字符串
错误,因为当a组合框
可编辑时,您正在写入的是一个文本字段
,它返回一个字符串
。当您从该字段“取消焦点”(例如按Alt+Tab,单击另一个字段等)或按“回车”时,系统会尝试将该字符串添加到组合框列表中,但由于所述组合框属于客户类型,因此无法执行
要管理它,您需要使用StringConverter
。看看如何实现这一点
编辑:
在摆弄之后,我发现。
我设法使它适应你的情况
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
是一个自定义转换器
类
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;
}
}
没有足够的信息来理解为什么会发生这种情况:创建一个项目并将其包含在内。您是通过fxml添加项目还是通过fxml设置初始值?ComboBox
可编辑吗?@fabian我通过fxml添加项目,ComboBox
可编辑您的Customer
类未正确注释,无法从fxml初始化实例。这可能就是String
s在items
列表中结束的方式。可编辑的组合框
es没有合适的转换器也可能导致此问题;不过我不是百分之百确定。谢谢你帮了我的忙!我用这个代码工作https://hastebin.com/ligolahomo.java
但它无法实现使用客户
对象而不是字符串
的目的,因为我无法在组合框中获取客户
的id。有没有办法解决这个问题?@Banjer_HD检查我的编辑。
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;
}
}
package sample;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.input.KeyCode;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
ComboBox<Customer> selectionBox;
private ArrayList<Customer> customerList = new ArrayList<Customer>();
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
selectionBox.getItems().addAll(addItemsToComboBox());
selectionBox.setConverter(new CustomerConverter());
// This will add the New Customer object from the ComboBox's TextField to the list of Customers
// on pressing ENTER
selectionBox.setOnKeyPressed(e -> {
if (e.getCode() == KeyCode.ENTER) {
// To create a new ID, get the total number of items in the ArrayList of Customer objects
// and add 1.
// You can also call your Order method here or read the ArrayList and get the Customer from there.
addCustomer(new Customer(customerList.size() + 1, selectionBox.getEditor().getText()));
}
});
// By default, when the ComboBox TextField is un-focused, the system will try to add it to the list
// You can tell by the 'System.out.print's in the two methods below this one, but by doing things this
// way, it will not be able to because your ComboBox is populated by the ArrayList.
// If you still want to add Customer Objects when it's un-focused, uncomment this part.
/*selectionBox.focusedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observableValue, Boolean outOfFocus, Boolean onFocus) {
if (outOfFocus) {
// You can also call your Order method here or read the ArrayList and get the Customer from there.
addCustomer(new Customer(customerList.size() + 1, selectionBox.getEditor().getText()));
}
}
});*/
// Handles the selected Customer item from the ComboBox.
selectionBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Customer>() {
public void changed(ObservableValue<? extends Customer> ov,
final Customer oldvalue, final Customer newvalue) {
// This IF check exists, because the first time you select an item,
// there will be no old item that has been previously selected.
// If this check is removed, you'll get a NullPointerException when trying to get values from
// 'oldvalue' Object.
if (oldvalue != null){
System.out.println("Customer changed from: " + oldvalue.getName()
+ " with ID: " +oldvalue.getId() + " to: "
+ newvalue.getName() + " with ID: " + newvalue.getId());
}
else {
System.out.println("New customer has been selected who's name is: "
+ newvalue.getName() + " with ID: " + newvalue.getId());
}
}
});
// Handles the selected Customer item index from the ComboBox.
selectionBox.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>()
{
public void changed(ObservableValue<? extends Number> ov,
final Number oldvalue, final Number newvalue) {
System.out.println("Old ComboBox Index: " + oldvalue + " changed to: " + newvalue);
}
});
}
// Adds a new Customer Object to the list.
// Clears the ComboBox and re-populates it.
public void addCustomer(Customer customer) {
customerList.add(customer);
selectionBox.getItems().clear();
selectionBox.getItems().addAll(customerList);
}
// Adds the initial Customer Objects for testing purposes.
public ArrayList<Customer> addItemsToComboBox(){
customerList = new ArrayList<Customer>();
customerList.add(new Customer(1,"John"));
customerList.add(new Customer(2,"Mark"));
customerList.add(new Customer(3,"Roger"));
customerList.add(new Customer(4,"David"));
customerList.add(new Customer(5,"Rick"));
return customerList;
}
}