TextField.bindAutoCompletion 返回对象 ID
TextField.bindAutoCompletion returning object ID
我有以下代码构建状态组合框。该代码按预期工作但是我无法自动完成以正常工作。似乎自动完成返回的是对象 ID 而不是预期的文本。单击下拉菜单会显示正确的州缩写名称。我哪里错了?
private void buildStateCombo() {
List<StatesDTO> states = GetStateList.getStatesList();
StateCombo.setItems(FXCollections.observableList(states));
StateCombo.setEditable(true);
TextFields.bindAutoCompletion(StateCombo.getEditor(), StateCombo.getItems());
/**
* StringConverter
*/
StateCombo.setConverter(new StringConverter<StatesDTO>() {
@Override
public String toString(StatesDTO object) {
if (object == null){
return null;
} else {
return object.getStateShort();
}
}
@Override
public StatesDTO fromString(String string) {
return DriverAddStateCombo.getItems().stream().filter(state ->
state.getStateShort().equals(string)).findFirst().orElse(null);
}
});
}
GetStateList 看起来像这样...
public class GetStateList {
public static List<StatesDTO> getStatesList() {
Database db = new Database();
List<StatesDTO> statesList = new ArrayList<>();
try {
String query = "SELECT stateID, stateCode FROM States";
ResultSet statesRS = db.executeQuery(query);
while(statesRS.next()) {
StatesDTO statesdto = new StatesDTO();
statesdto.setStateID(statesRS.getString(1));
statesdto.setStateShort(statesRS.getString(2));
statesList.add(statesdto);
}
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
db.closeConnection();
return statesList;
}
}
和 StatesDTO
public class StatesDTO {
public SimpleStringProperty stateID = new SimpleStringProperty();
public SimpleStringProperty stateShort = new SimpleStringProperty();
public String getStateID() {
return stateID.get();
}
public void setStateID(String stateIDStr) {
stateID.set(stateIDStr);
}
public String getStateShort() {
return stateShort.get();
}
public void setStateShort(String stateShortStr) {
stateShort.set(stateShortStr);
}
}
我可以通过一种方法直接在集合中提供名称。
TextFields.bindAutoCompletion(StateCombo.getEditor(),
StateCombo.getItems().stream().map(state ->
state.getStateShort()).collect(Collectors.toList()) );
我尝试了不同的方法签名,例如
bindAutoCompletion(TextField textField, Callback> suggestionProvider, StringConverter converter)
但我无法让它工作。
注意:ControlsFX 版本 8.40.12。
我有以下代码构建状态组合框。该代码按预期工作但是我无法自动完成以正常工作。似乎自动完成返回的是对象 ID 而不是预期的文本。单击下拉菜单会显示正确的州缩写名称。我哪里错了?
private void buildStateCombo() {
List<StatesDTO> states = GetStateList.getStatesList();
StateCombo.setItems(FXCollections.observableList(states));
StateCombo.setEditable(true);
TextFields.bindAutoCompletion(StateCombo.getEditor(), StateCombo.getItems());
/**
* StringConverter
*/
StateCombo.setConverter(new StringConverter<StatesDTO>() {
@Override
public String toString(StatesDTO object) {
if (object == null){
return null;
} else {
return object.getStateShort();
}
}
@Override
public StatesDTO fromString(String string) {
return DriverAddStateCombo.getItems().stream().filter(state ->
state.getStateShort().equals(string)).findFirst().orElse(null);
}
});
}
GetStateList 看起来像这样...
public class GetStateList {
public static List<StatesDTO> getStatesList() {
Database db = new Database();
List<StatesDTO> statesList = new ArrayList<>();
try {
String query = "SELECT stateID, stateCode FROM States";
ResultSet statesRS = db.executeQuery(query);
while(statesRS.next()) {
StatesDTO statesdto = new StatesDTO();
statesdto.setStateID(statesRS.getString(1));
statesdto.setStateShort(statesRS.getString(2));
statesList.add(statesdto);
}
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
db.closeConnection();
return statesList;
}
}
和 StatesDTO
public class StatesDTO {
public SimpleStringProperty stateID = new SimpleStringProperty();
public SimpleStringProperty stateShort = new SimpleStringProperty();
public String getStateID() {
return stateID.get();
}
public void setStateID(String stateIDStr) {
stateID.set(stateIDStr);
}
public String getStateShort() {
return stateShort.get();
}
public void setStateShort(String stateShortStr) {
stateShort.set(stateShortStr);
}
}
我可以通过一种方法直接在集合中提供名称。
TextFields.bindAutoCompletion(StateCombo.getEditor(),
StateCombo.getItems().stream().map(state ->
state.getStateShort()).collect(Collectors.toList()) );
我尝试了不同的方法签名,例如
bindAutoCompletion(TextField textField, Callback> suggestionProvider, StringConverter converter)
但我无法让它工作。
注意:ControlsFX 版本 8.40.12。