gpt4 book ai didi

JavaFx Combobox 值表现得很奇怪

转载 作者:行者123 更新时间:2023-11-30 02:03:32 25 4
gpt4 key购买 nike

我一直在学习javafx,我正在尝试组合框,但似乎无法正确地从组合框中获取输出。当我尝试使用组合框中的值作为字符串时,它给出了 ClassCastException: java.lang.Integer 无法转换为 java.lang.String,当我尝试将该值用作 int 或 Integer(都尝试过)时,它给了我相反的 ClassCastException: java.lang.String 无法转换为 java.lang.Integer。

我尝试使用获取值

comboBox.getSelectionModel().getSelectedItem();

还有

comboBox.getValue();

我尝试使用 valueOf、parseInt 和 toString 显式转换值。使用 getClass 还会给出 ClassCastException: java.lang.String 无法转换为 java.lang.Integer..

这是我一直在使用的组合框:

<ComboBox fx:id="comboBox"  editable="true" promptText="Enter Period in Days"  >
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:id="week" fx:value="7" />
<String fx:id="fortnite" fx:value="14" />
<String fx:id="month" fx:value="30" />
<String fx:id="monthx3" fx:value="90" />
<String fx:id="year_2" fx:value="180" />
<String fx:id="year" fx:value="365"/>
</FXCollections>
</items>
</ComboBox>

如何从该组合框中获取值?我做错了什么?

最佳答案

如果您使用的类型与 String 不同,并且希望保持 ComboBox 可编辑,则需要将 StringConverter 分配给ComboBox.converter属性能够将String转换为ComboBox的项目类型。否则,当 ComboBox 尝试解析组合框 TextField 的输入时,您将收到 ClassCastException

注意:向 fxml 中的元素添加 fx:id 属性不会导致 fx:id 与为该元素创建的对象组合使用。相反,它所做的只是允许您将实例注入(inject) Controller 中的字段或稍后在 fxml 中引用该实例。

因为您似乎想保留 2 条信息(Stringint),既不是 String 也不是 Integer可能适合你。您可以创建自定义类型:

public class NamedDuration {
private final int days;
private final String name;

public NamedDuration(@NamedArg("days") int days, @NamedArg("name") String name) {
this.days = days;
this.name = name;
}

public int getDays() {
return days;
}

public String getName() {
return name;
}

@Override
public String toString() {
return name;
}

}
<ComboBox fx:id="comboBox" editable="true" onAction="#comboChange" promptText="Enter Period in Days">
<items>
<FXCollections fx:factory="observableArrayList">
<NamedDuration name="week" days="7"/>
<NamedDuration name="fortnite" days="14"/>
<NamedDuration name="month" days="30"/>
<NamedDuration name="monthx3" days="90"/>
<NamedDuration name="year_2" days="180"/>
<NamedDuration name="year" days="365"/>
</FXCollections>
</items>
</ComboBox>

Controller 类

public class FXML2Controller {

@FXML
private ComboBox<NamedDuration> comboBox;

@FXML
private void comboChange() {
NamedDuration duration = comboBox.getValue();
if (duration != null) {
System.out.format("%d days = %s\n", duration.getDays(), duration.getName());
}
}

@FXML
private void initialize() {
// set converter to convert between String and NamedDuration
comboBox.setConverter(new StringConverter<NamedDuration>() {

@Override
public String toString(NamedDuration object) {
return object == null ? "" : object.getName();
}

@Override
public NamedDuration fromString(String string) {
if (string == null || string.isEmpty()) {
return null;
}

// try matching names
for (NamedDuration nd : comboBox.getItems()) {
if (nd.getName().equalsIgnoreCase(string)) {
return nd;
}
}

// try matching number
int days;
try {
days = Integer.parseInt(string);
} catch (NumberFormatException ex) {
return null;
}
for (NamedDuration nd : comboBox.getItems()) {
if (days == nd.getDays()) {
return nd;
}
}

return null;
}

});
}

}

关于JavaFx Combobox 值表现得很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52004338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com