无法以编程方式选择使用对象的Javafx组合框项目 [英] Unable select Javafx combobox items programmatically which uses objects
问题描述
在使用类对象列表的JavaFx ComboBox中.我想使用getSelectionModel().select(object或index)以编程方式在ComboBox中选择项目.我没有得到期望的结果尽管设置了值,但是它类似于main.dao.Company.Company.CompanyTableData@74541e7b.代码有点像这样.
In the JavaFx ComboBox which uses a class object list .I want to select items in the ComboBox programmatically using getSelectionModel().select(object or index). i am not getting the desired result Although the value is set but it is something like this main.dao.Company.Company.CompanyTableData@74541e7b. The code is somewhat like this.
ComboBox<CompanyTableData> company = new ComboBox<>();
company.setItems(GetCompany.getCompanyTableData());//where Observable list is set..
GetCompany.getCompanyTableData()返回CompanyTableData类的可观察列表.
GetCompany.getCompanyTableData() returns observablelist of CompanyTableData class.
组合框的外观如下.
CompanyTableData类为.
The CompanyTableData Class is as.
public class CompanyTableData {
private SimpleStringProperty itemCompanyId;
private SimpleStringProperty itemCompanyName;
private SimpleStringProperty createBy;
private SimpleStringProperty createdOn;
public CompanyTableData(CompanyData companyData){
this.itemCompanyId = new SimpleStringProperty(companyData.getItemCompanyId());
this.itemCompanyName = new SimpleStringProperty(companyData.getItemCompanyName());
this.createBy = new SimpleStringProperty(companyData.getCreatedBy());
this.createdOn = new SimpleStringProperty(companyData.getCreatedOn());
}
public String getItemCompanyId() {
return itemCompanyId.get();
}
public SimpleStringProperty itemCompanyIdProperty() {
return itemCompanyId;
}
public void setItemCompanyId(String itemCompanyId) {
this.itemCompanyId.set(itemCompanyId);
}
public String getItemCompanyName() {
return itemCompanyName.get();
}
public SimpleStringProperty itemCompanyNameProperty() {
return itemCompanyName;
}
public void setItemCompanyName(String itemCompanyName) {
this.itemCompanyName.set(itemCompanyName);
}
public String getCreateBy() {
return createBy.get();
}
public SimpleStringProperty createByProperty() {
return createBy;
}
public void setCreateBy(String createBy) {
this.createBy.set(createBy);
}
public String getCreatedOn() {
return createdOn.get();
}
public SimpleStringProperty createdOnProperty() {
return createdOn;
}
public void setCreatedOn(String createdOn) {
this.createdOn.set(createdOn);
}
}
细胞工厂已设置
company.setCellFactory(param -> new CompanyCell());
还有CompanyCell
And the CompanyCell
public class CompanyCell extends ListCell<CompanyTableData> {
@Override
protected void updateItem(CompanyTableData item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null || item.getItemCompanyName() == null) {
setText(null);
} else {
setText(item.getItemCompanyName());
}
}
}
在所有这些之后,当我尝试以编程方式将项目设置为
After all this when i try to set the items programmetically as
company.getSelectionModel().select(getSelectedCompanyIndex());
getSelectedCompanyIndex()函数如下.
The getSelectedCompanyIndex() function is as follows.
public static CompanyTableData getSelectedCompanyIndex(){
CompanyTableData c = null,i;
Iterator<CompanyTableData> itr = GetCompany.getCompanyTableData().iterator();
while (itr.hasNext()){
i = itr.next();
if (i.getItemCompanyName().equals(Element.getItemTableData().getCompany())){
c = i;
}
}
return c;
}
我得到的结果是和最后,它应该在列表中选择一个名称或项目,但我设置了某种类型的对象.
And the result i am getting is And At the end it should select a name or item in the list but it has set some type of object i think.
现在我该怎么办.是否需要任何类型的字符串转换.
Now what should i do. Is there any type of string conversion required.
推荐答案
在未显示组合框弹出窗口时用于显示项目的 buttonCell
不会使用 cellFactory自动创建.代码>.您还需要设置此属性以使用相同的单元实现:
The buttonCell
used to display the item when the combobox popup is not shown is not automatically created using the cellFactory
. You need to set this property too to use the same cell implementation:
company.setCellFactory(param -> new CompanyCell());
company.setButtonCell(new CompanyCell());
这篇关于无法以编程方式选择使用对象的Javafx组合框项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!