JFXComboBox不显示默认设置值

时间:2018-02-08 11:22:25

标签: javafx java-8

我正在使用JFXComboBox来显示可以分配用户的用户组。角色是从DB获取的。 role获取getRoleNameList()列表由// Populate userGroup JFXComboBox ObservableList<Role> roles = rdc.getRoleNameList(); roles.sort(Comparator.comparing(Role::getCode)); // Sort the list uGroupComboBox.getItems().setAll(roles); uGroupComboBox.setVisibleRowCount(5); 完成。我填写JFXComboBox如下:

填充userGroup JFXComboBox

<JFXComboBox id="userGroup" fx:id="uGroupComboBox" focusColor="#07595a" 
    layoutX="245.0" layoutY="256.0" prefHeight="30.0" prefWidth="148.0"
    promptText="Select User Group" styleClass="jfx-combo-box" 
    unFocusColor="#48aaad">

JFXCombobox FXML:

package records.models;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import org.apache.commons.lang3.StringUtils;

public class Role {
private final StringProperty code;
private final StringProperty name;

public Role() {
    this.name = new SimpleStringProperty();
    this.code = new SimpleStringProperty();
}

public Role(String name) {
    String codename = StringUtils.capitalize(name.toLowerCase().trim());
    this.code = new SimpleStringProperty(codename);

    name = name.toLowerCase().trim();
    this.name = new SimpleStringProperty(name);
}

// name
public String getName() {
    return name.get();
}

public void setName(String name) {
    if (name != null) {
        name = name.toLowerCase().trim();
    }
    this.name.set(name);
}

public StringProperty nameProperty() {
    return name;
}

// @return code
public String getCode() {
    return code.get();
}

public void setCode(String code) {
    if (code != null) {
        code = StringUtils.capitalize(code.toLowerCase().trim());
    }
    this.code.set(code);
}

public StringProperty codeProperty() {
    return code;
}

public Role getRole(){
    return this;
}

@Override
public String toString(){
    return this.code.get();
}

以下模型适用于角色:

String uRole = role.get("role") == null ? null : 
    role.get("role").toString();
user.setRole(uRole);
// Create the role that is fetched from DB
Role uGroup = new Role(uRole);
// uGroupComboBox.setValue(uGroup);
uGroupComboBox.getSelectionModel().select(uGroup);

}

当我从数据库中提取用户所在的用户组时,不会显示数据:

System.out.println(uGroupComboBox.getValue());

这不起作用。如您所见,该字段不为空。如果是,我会显示提示文字

This does not work. As you can see, the field is not null. If it were, I would show the prompt text

但是,JFXComboBox列表已成功填充:

enter image description here

如果我Nurse,它会显示正确的值,例如。 如果用户组为Nurse,则语句返回getProgress()

为什么JFXComboBox不显示值?

1 个答案:

答案 0 :(得分:0)

问题出在您的Role对象及其StringProperty属性中。让我解释一下自己。

如果您只是服用:

static void main(String[] args){

    StringProperty codeName = new SimpleStringProperty("codename");
    StringProperty codeNameSameValue = new SimpleStringProperty("codename");
    boolean isthesame = codeName.equals(codeNameSameValue);
    System.out.println("isthesame:"+isthesame);
    boolean isReallyTheSame = codeName.get().equals(codeNameSameValue.get());
    System.out.println("isReallyTheSame:"+isReallyTheSame);

}

您将在控制台输出上看到

isthesame:false
isReallyTheSame:true

好吧,仍然不是您问题的答案,但对您来说,Role类的默认equals方法将像我的第一次比较那样比较对象属性:

comparingStringProperty.equals(comparedStringProperty);  
...

要解决此问题,您需要重写Role对象中的equals方法。例如,这样的事情: 我

mport java.util.Objects;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import org.apache.commons.lang3.StringUtils;

public class Role {

    private final StringProperty code;
    private final StringProperty name;

    /*Constructors, setters and getter are deleted for clarity */
    ....

    @Override
    public String toString() {
        return this.code.get();
    }

    @Override
    public boolean equals(Object o) {

        // If the object is compared with itself then return true   
        if (o == this) {
            return true;
        }

        /* Check if o is an instance of Role or not 
          "null instanceof [type]" also returns false */
        if (!(o instanceof Role)) {
            return false;
        }

        // typecast o to Role so that we can compare data members  
        Role r = (Role) o;

        return (r.name.get() == this.name.get() || r.name.get() != null && r.name.get().equals(this.name.get()));
    }

    @Override
    public int hashCode() {
        return Objects.hash(name.get(), code.get());
    }
}

此equals方法将在调用期间使用:

uGroupComboBox.getSelectionModel().select(uGroup);

瞧!