[英]JavaFx Combobox value acting wierd
我一直在學習javafx,正在嘗試combobox,但似乎無法正確地從combobox中獲取輸出。 當我嘗試使用combobox中的值作為String時,它給了我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
類型不同於String
並且想要使ComboBox
可編輯,則需要將StringConverter
分配給ComboBox.converter
屬性,該屬性能夠將String
轉換為ComboBox
的項目類型。 否則,當ComboBox
嘗試解析組合框TextField
的輸入時,您將獲得ClassCastException
。
注意:向fxml中的元素添加fx:id
屬性不會導致fx:id
和為要使用的元素創建的對象組合。 相反,它所做的只是允許您將實例注入控制器中的某個字段,或者稍后在fxml中引用該實例。
由於您似乎想保留2條信息( String
和int
),因此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>
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;
}
});
}
}
您的FXCollections返回String,因為您在集合中聲明了String。 如果您想要整數,請嘗試以下操作:
<ComboBox fx:id="comboBox" editable="true" promptText="Enter Period in Days" >
<items>
<FXCollections fx:factory="observableArrayList">
<Integer fx:id="week" fx:value="7" />
<Integer fx:id="fortnite" fx:value="14" />
<Integer fx:id="month" fx:value="30" />
<Integer fx:id="monthx3" fx:value="90" />
<Integer fx:id="year_2" fx:value="180" />
<Integer fx:id="year" fx:value="365"/>
</FXCollections>
</items>
</ComboBox>
還有comboBox.getValue(); 應該返回整數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.