【发布时间】:2020-02-20 05:50:17
【问题描述】:
我正在尝试创建一个 JavaFX 程序来管理生产线。我正在使用数据库,以便用户可以使用TextField 和ChoiceBox 填充数据库。我无法从我创建的 Enum 类中填写 ChoiceBox。
预计我的ChoiceBox 填充了我的Enum 类中的项目,但是ChoiceBox 保持空白。代码正在编译。
在您将我推荐给 this link 之前,我已尝试使用该讨论中的建议,但我无法理解它。该讨论无济于事,因为我不确定他们试图在哪里填充他们的ComboBox。此外,cbxStatus.getItems().setAll(Status.values()); 对我不起作用(我可能应用不正确)。
我的枚举类:
package sample;
public enum ItemType {
AUDIO("AU"),
VISUAL("VI"),
AUDIOMOBILE("AM"),
VISUALMOBILE("VM");
final String itemType;
ItemType(String itemType) {
this.itemType = itemType;
}
public String getItemType() {
return itemType;
}
}
我的控制器类:
package sample;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import java.sql.SQLException;
public class Controller {
@FXML
private TextField productName;
@FXML
private TextField productManufacturer;
@FXML
private ChoiceBox<ItemType> itemType = new ChoiceBox<>();
private DatabaseManager databaseManager = new DatabaseManager();
public Controller() throws SQLException {
}
@FXML
public void initialize() {
itemType.getItems().setAll(ItemType.values());
}
public void addProduct() {
String name = productName.getText();
String manufacturer = productManufacturer.getText();
String type = itemType.toString();
databaseManager.insert(type, manufacturer, name);
System.out.println("Button Pressed");
}
public void recordProduction(ActionEvent actionEvent) {
System.out.println("Button Pressed");
}
}
最后,一旦ChoiceBox 被填充,我需要捕获用户的选择以输入到我的数据库中。类似于String type = itemType.toString();。
【问题讨论】:
标签: java sql database javafx enums