Как я могу поместить текстовые поля в ListView - PullRequest
0 голосов
/ 08 апреля 2019

EDIT: У меня есть графический интерфейс с ListView с четырьмя TextFields и двумя кнопками «+» и «-» Поэтому я хотел бы добавить больше строк с этими элементами с помощью кнопки + и удалить его с помощью кнопки -.

Я могу отобразить одну строку с элементами, но не могу добавить еще.

Я не знаю, где моя ошибка. Надеюсь, кто-нибудь может мне помочь.

Вот мой код с классом модели.

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;
    }

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...