以下代码应允许用户输入他们要添加到列表中的新产品的详细信息(Choicebox除外)。几乎所有的代码都可以工作,只有方法handlePrice()。每当用户尝试输入priceText TextField时,错误"空字符串"被抛出并且不允许用户输入任何字符。所有变量都在代码中初始化,PartList是初始化的ObservableList。
//TextFields
@FXML
private TextField partNameText;
@FXML
private TextField brandNameText;
@FXML
private TextField itemIDText;
@FXML
private TextField materialText;
@FXML
private TextField priceText;//
@FXML
private void handlePartType() {
partType = partTypeChoice.getValue();
}
//
@FXML
private void handlePartName() {
partName = partNameText.getText();
}
//
@FXML
private void handleBrandName() {
brandName = brandNameText.getText();
}
//
@FXML
private void handleItemID() {
itemID = itemIDText.getText();
}
//
@FXML
private void handleMaterial() {
material = materialText.getText();
}
//
@FXML
private void handlePrice() {
String priceString = priceText.getText();
price = Double.parseDouble(priceString);
}
//
@FXML
private void newPart() {
//
if((partTypeChoice.getValue().isEmpty() == false)&&(partNameText.getText().isEmpty() == false)) {
//
PartList.add(partListIndex, new Part(partType, partName, brandName, itemID, material, price));
partListIndex++;
//
partListTable.setItems(PartList);
}
else {
//Changes the text of the Required labels
partTypeLabel.setText("*Part Type");
partNameLabel.setText("*Part Name");
//Changes the color of the Required Labels
partTypeLabel.setTextFill(javafx.scene.paint.Color.RED);
partNameLabel.setTextFill(javafx.scene.paint.Color.RED);
//Displays the warning
warningLabel.setVisible(true);
dismissButton.setVisible(true);
}
}