[英]combobox integer to observablelist
I'm having problems setting an Integer value to an observable list. 我在将Integer值设置为可观察列表时遇到问题。
I have the following code, and some Integer values in a combobox. 我有以下代码,以及组合框中的一些Integer值。 When pressing the button I want to add the Integer from the combobox to the observable list. 当按下按钮时,我想将组合框中的整数添加到可观察列表。 I'm able to do that with Strings, but this doesn't work with Integer. 我可以使用Strings来做到这一点,但这不适用于Integer。 I also need to get the data in as integers as I need to do some calculation. 我还需要以整数形式获取数据,因为我需要进行一些计算。 Does anyone have an idea? 有人有主意吗?
public class Resultat {
public SimpleStringProperty res_Auswärtsteam = new SimpleStringProperty();
public SimpleIntegerProperty res_Auswärtstore = new SimpleIntegerProperty();
public Integer getRes_auswärtstore() {
return res_Auswärtstore.get();
}
public String getRes_auswärtsteam() {
return res_Auswärtsteam.get();
}
} }
public class Resultat_Controller implements Initializable {
// The table and columns
@FXML TableView<Resultat> result_tableview;
@FXML TableColumn result_Auswärtstore;
@FXML TableColumn result_Auswärtsteam;
@FXML Button result_btn_Hinzufügen;
@FXML ComboBox result_cbo_Auswärtstore;
@FXML ComboBox result_cbo_Auswärtsteam;
// The table's data
ObservableList<Resultat> data;
@Override
public void initialize(URL url, ResourceBundle rb) {
// Set up the table data
result_Auswärtstore.setCellValueFactory(new PropertyValueFactory<Resultat, Integer>("res_auswärtstore"));
result_Auswärtsteam.setCellValueFactory(new PropertyValueFactory<Resultat, Integer>("res_auswärtsteam"));
data = FXCollections.observableArrayList();
result_tableview.setItems(data);
result_cbo_Auswärtstore.getItems().clear();
result_cbo_Auswärtstore.getItems().addAll("1", "2", "3", "4");
result_cbo_Auswärtsteam.getItems().clear();
result_cbo_Auswärtsteam.getItems().addAll("String 1", "String 2", "String 3", "String 4");
}
@FXML
private void handleButtonAction(ActionEvent event) {
Resultat resultateintrag = new Resultat();
resultateintrag.res_Auswärtsteam.setValue(result_cbo_Auswärtsteam.getValue().toString());
resultateintrag.res_Auswärtstore.setValue(2);
data.add(resultateintrag);
}
} }
At the moment I set a default value of 2 (which should obviously go away). 目前,我将默认值设置为2(显然应该消失了)。 Thanks 谢谢
Your @FXML
fields lack generic types. 您的@FXML
字段缺少通用类型。 Change ComboBox
to ComboBox<Integer>
and ComboBox<String>
. 改变ComboBox
以ComboBox<Integer>
和ComboBox<String>
。 Similarly, the TableColumn fields should be TableColumn<Resultat, Integer>
and TableColumn<Resultat, String>
respectively. 同样,TableColumn字段应分别为TableColumn<Resultat, Integer>
和TableColumn<Resultat, String>
。 The documentation for ComboBox and TableColumn has more information. ComboBox和TableColumn的文档具有更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.