编辑: 我有一个带有ListView的GUI,该列表具有四个TextField和两个按钮“ +”和“-” 因此,我想使用+按钮向此元素添加更多行,并通过-按钮将其删除。
我可以在元素上显示一行,但不能再添加更多。
我不知道我的错误在哪里。希望有人可以帮助我。
这是我的模型类代码。
public class RowListCell extends ListCell<Model> {
private ObservableList<Model> rows = FXCollections.observableArrayList();
CreateSchemeView csv = new CreateSchemeView(null);
private HBox content = new HBox();
private TextField tf0 = new TextField();
private TextField tf1 = new TextField();
private TextField tf2 = new TextField();
private TextField tf3 = new TextField();
private Button add = new Button("+");
private Button sub = new Button("-");
Model model = new Model(tf0, tf1, tf2, tf3);
public RowListCell() {
content.getChildren().addAll(tf0, tf1, tf2, tf3, add, sub);
content.setSpacing(10);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
setGraphic(content);
}
protected void updateItem(model.Model item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
setGraphic(null);
} else {
setGraphic(content);
add.setOnAction(e -> {
Model newRow = new Model(tf0, tf1, tf2, tf3);
rows.add(newRow);
});
sub.setOnAction(e -> {
rows.remove(item);
});
}
}
}
public class CreateSchemeView extends VBox {
Model model = new Model();
private final Button view = new Button("Zurück");
private ObservableList<Model> rows = FXCollections.observableArrayList();
public ListView<Model> list = new ListView<>();
StackPane root = new StackPane();
Pane pane = new Pane();
VBox vbox = new VBox();
HBox hbox = new HBox();
Button save = new Button("Speichern");
public CreateSchemeView(final CreateSchemeController createSchemeController) {
initCreateScheme();
view.setOnAction(event -> {
System.out.println("ButtonB has been pressed, switching to ViewA.");
final Stage primaryStage = createSchemeController.getPrimaryStage();
final ViewController viewController = new ViewController(primaryStage);
final Scene scene = new Scene(viewController.getView(),250,600);
scene.getStylesheets().add("style.css");
primaryStage.setScene(scene);
});
}
public void initCreateScheme(){
vbox.setLayoutX(0);
vbox.setLayoutY(0);
vbox.setMaxWidth(200);
vbox.setMaxHeight(800);
hbox.setLayoutX(205);
rows.add(model);
list.setCellFactory(c -> new RowListCell());
list.setItems(rows);
vbox.getChildren().addAll(view, save);
vbox.setPadding(new Insets(10,10,10,10));
vbox.setSpacing(5);
root.setLayoutX(205);
root.setLayoutY(10);
root.setPrefWidth(900);
root.setPrefHeight(580);
root.getChildren().addAll(list);
hbox.getChildren().addAll(root);
hbox.setLayoutY(10);
pane.getChildren().addAll(vbox, hbox);
this.getChildren().addAll(pane);
}
public Button getCreateSchemeView() {
return view;
}
}
public class Model {
private TextField tf0;
private TextField tf1;
private TextField tf2;
private TextField tf3;
public Model(TextField tf0, TextField tf1, TextField tf2, TextField tf3) {
this.tf0 = tf0;
this.tf1 = tf1;
this.tf2 = tf2;
this.tf3 = tf3;
}
public Model() {}
public TextField getTF0() {
return tf0;
}
public void setTF0(TextField tf0) {
this.tf0 = tf0;
}
public TextField getTF1() {
return tf1;
}
public void setTF1(TextField tf1) {
this.tf1 = tf1;
}
public TextField getTF2() {
return tf2;
}
public void setTF2(TextField tf2) {
this.tf2 = tf2;
}
public TextField getTF3() {
return tf3;
}
public void setTF3(TextField tf3) {
this.tf3 = tf3;
}
}