我正在尝试制作一个可编辑的表,让用户输入值。值输入有效,但是当用户进行编辑时,它不会保存并输出错误消息。这是我的代码:
public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setTitle("Table View Sample");
stage.setWidth(450);
stage.setHeight(550);
final Label label = new Label("Data");
label.setFont(new Font("Arial", 20));
table.setEditable(true);
TableColumn aCol = new TableColumn("a");
aCol.setMinWidth(100);
aCol.setCellValueFactory(
new PropertyValueFactory<>("A"));
aCol.setCellFactory(TextFieldTableCell.<info, Double>forTableColumn(new DoubleStringConverter()));
aCol.setOnEditCommit(
new EventHandler<CellEditEvent<info, String>>() {
@Override
public void handle(CellEditEvent<info, String> t) {
((info) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setA(Double.parseDouble(t.getNewValue()));
}
}
);
TableColumn bCol = new TableColumn("B");
bCol.setMinWidth(200);
bCol.setCellValueFactory(
new PropertyValueFactory<>("b"));
bCol.setCellFactory(TextFieldTableCell.<info, Double>forTableColumn(new DoubleStringConverter()));
bCol.setOnEditCommit(
new EventHandler<CellEditEvent<info, String>>() {
@Override
public void handle(CellEditEvent<info, String> t) {
((info) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setB(Double.parseDouble(t.getNewValue()));
}
}
);
table.setItems(data);
table.getColumns().addAll(aCol,bCol);
final TextField addA = new TextField();
addA.setPromptText("A");
addA.setMaxWidth(aCol.getPrefWidth());
final TextField addB = new TextField();
addB.setMaxWidth(BCol.getPrefWidth());
addB.setPromptText("B");
final Button addButton = new Button("Add");
addButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
data.add(new info(
Double.parseDouble( addA.getText()),
Double.parseDouble(addB.getText())));
addA.clear();
addB.clear();
}
});
hb.getChildren().addAll(addB, addA, addButton);
hb.setSpacing(3);
final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, table, hb);
((Group) scene.getRoot()).getChildren().addAll(vbox);
stage.setScene(scene);
stage.show();
}
我的错误信息是:
Exception in thread "JavaFX Application Thread" java.lang.ClassCastException: java.lang.Double
cannot be cast to java.lang.String
提前感谢任何人可以提供给我的任何帮助