我无法将我的字符串值打印到我构建的GUI ComboBox中。基本上,每次我将cbGame
ComboBox的值分配给JavaFX程序时,当我尝试启动GUI时,它会给我一个非法的参数异常。完整的代码在我的GitHub上:https://github.com/jparr721/Blue-Box2
我需要帮助的代码如下。
控制器类:
public class RentGameDialogController extends RentalStoreGUIController implements Initializable{
@FXML private TextField nameField, rentedOn, dueBack;
String name;
Date dateRentedOn, dateDue;
GregorianCalendar rented, due;
@FXML private ObservableList<String> cbGameOptions;
@FXML private ComboBox<String> cbGame;
@FXML ComboBox<PlayerType> cbConsole;
@FXML Button cancel, addToCart;
int counter = 0;
private Stage currentStage;
@Override
public void initialize(URL location, ResourceBundle resources) {
cbGameOptions = FXCollections.observableArrayList();
cbGameOptions.add("The Witcher 3");
cbGameOptions.add("Call of Duty: Advanced Warfare");
cbGameOptions.add("Call of Duty: Infinite Warfare");
cbGameOptions.add("The Elder Scrolls IV: Skyrim");
cbGame.setItems(cbGameOptions);
}
public void getName(){
name = nameField.getText();
try {
String[] firstLast = name.split(" ");
String firstName = firstLast[0];
String lastName = firstLast[1];
} catch (Exception e){
e.printStackTrace();
}
}
public void getRentedOn() throws ParseException {
DateFormat format = new SimpleDateFormat("MM/dd/yy");
dateRentedOn = format.parse(rentedOn.getText());
rented.setTime(dateRentedOn);
}
public void getDueBack() throws ParseException {
DateFormat format = new SimpleDateFormat("MM/dd/yy");
dateDue = format.parse(dueBack.getText());
due.setTime(dateDue);
}
@FXML
public void handleCancelButtonAction (ActionEvent event) {
currentStage = (Stage) cancel.getScene().getWindow();
currentStage.close();
}
@FXML
public void addToCartButton (ActionEvent event) {
currentStage = (Stage) cancel.getScene().getWindow();
currentStage.close();
}}
答案 0 :(得分:1)
我知道这个问题,我打开你的代码,发现你没有在fxml中使用comboBox,所以我用组合框更改它,你使用的是SplitMenuButton。
@Override
public void initialize(URL location, ResourceBundle resources) {
cbGameOptions = FXCollections.observableArrayList();
cbGameOptions.add("The Witcher 3");
cbGameOptions.add("Call of Duty: Advanced Warfare");
cbGameOptions.add("Call of Duty: Infinite Warfare");
cbGameOptions.add("The Elder Scrolls IV: Skyrim");
cbGame.getItems().addAll(cbGameOptions);
}
答案 1 :(得分:0)
好的,首先删除您撰写的cbGame.setItems(cbGameOptions);
你必须改为
cbGame.getItems().addAll(cbGameOptions);
答案 2 :(得分:-1)
我认为你不需要一个ObservableList的@FXML
注释。 @FXML
标记使FXMLLoader能够注入FXML中定义的值,但您的observableList不需要由FXMLLoader设置
所以
@FXML private ObservableList<String> cbGameOptions;
应该是
private ObservableList<String> cbGameOptions;
希望这有帮助。