首页 > 解决方案 > 带有 TextFields 和 TableView 的问题

问题描述

你好(回到那些以前帮助过我的人),

我对我的代码提出了一个新问题。所以之前,我有一个问题,在我的 tableview 上添加我的组合框值,我成功地修复了它。但是现在,我不能在我的表格中添加我的文本字段“文本”(这以前不是问题),所以你能再帮我一次吗?(我是法国人,首先,对不起我的英语,其次,我试图用英语翻译我所有的论点和其他人,以便更好地理解)。

我给你链接一张实际结果的图片: Result with text in the 2 TextFields

public class Tableviewex extends Application {

private TableView<Shop> table = new TableView<>();
private ObservableList<Shop> data =
        FXCollections.observableArrayList();
private ObservableList<String> productlist = FXCollections.observableArrayList("Doliprane","Spasfon");
private Pane layout = new Pane();
private HBox hb = new HBox();

public static void main(String[] args) {
    launch(args);
}

@Override
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("Choose your medication ");
    label.setFont(new Font("Arial", 20));



    TableColumn<Shop, String> productcol = 
        new TableColumn<>("Product");
    productcol.setMinWidth(200);
    productcol.setCellValueFactory(
        new PropertyValueFactory<>("product"));



   productcol.setCellFactory(TextFieldTableCell.<Shop>forTableColumn());
    productcol.setOnEditCommit(
        (CellEditEvent<Shop, String> t) -> {
            ((Shop) t.getTableView().getItems().get(
                t.getTablePosition().getRow())
                ).setProduct(t.getNewValue());
    });


    TableColumn<Shop, String> quantityCol = 
        new TableColumn<>("Quantité");
    quantityCol.setMinWidth(100);
    quantityCol.setCellValueFactory(
        new PropertyValueFactory<>("Quantité"));
    quantityCol.setCellFactory(TextFieldTableCell.<Shop>forTableColumn());
    quantityCol.setOnEditCommit(
        (CellEditEvent<Shop, String> t) -> {
            ((Shop) t.getTableView().getItems().get(
                    t.getTablePosition().getRow())
                    ).setQuantity(t.getNewValue());
    });

    TableColumn<Shop, String> priceCol = new TableColumn<>("Prix");
    priceCol.setMinWidth(100);
    priceCol.setCellValueFactory(
        new PropertyValueFactory<>("Prix"));
    priceCol.setCellFactory(TextFieldTableCell.<Shop>forTableColumn());       
    priceCol.setOnEditCommit(
        (CellEditEvent<Shop, String> t) -> {
            ((Shop) t.getTableView().getItems().get(
                    t.getTablePosition().getRow())
                    ).setEmail(t.getNewValue());
    });

    table.setItems(data);
    table.getColumns().addAll(productcol, quantityCol, priceCol);

    final ComboBox<String> choixproduit = new ComboBox<>();
    choixproduit.setItems(productlist);
    choixproduit.setPromptText("Choose your medication");
    choixproduit.setMaxWidth(productcol.getPrefWidth());
    final TextField addQuantity = new TextField();
    addQuantity.setMaxWidth(quantityCol.getPrefWidth());
    addQuantity.setPromptText("Quantity");
    final TextField addPrice = new TextField();
    addPrice.setMaxWidth(priceCol.getPrefWidth());
    addPrice.setPromptText("Price");

    final Button addButton = new Button("ADD");
    addButton.setOnAction((ActionEvent e) -> {
        data.add(new Shop(
                choixproduit.getValue(),
                addQuantity.getText(),
                addPrice.getText()));
        addQuantity.clear();
        addPrice.clear();
    });

    hb.getChildren().addAll(choixproduit, addQuantity, addPrice, 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,layout);

    stage.setScene(scene);
    stage.show();
}

public static class Shop {

    private final SimpleStringProperty product;
    private final SimpleStringProperty quantity;
    private final SimpleStringProperty price;

    private Shop(String fProduct, String quantity, String price) {
        this.product = new SimpleStringProperty(fProduct);
        this.quantity = new SimpleStringProperty(quantity);
        this.price = new SimpleStringProperty(price);
    }

    public String getProduct() {
        return product.get();
    }

    public void setProduct(String fProduct) {
        product.set(fProduct);
    }

    public String getQuantity() {
        return quantity.get();
    }

    public void setQuantity(String fProduct) {
        quantity.set(fProduct);
    }

    public String getEmail() {
        return price.get();
    }

    public void setEmail(String fProduct) {
        price.set(fProduct);
    }
}
}

标签: javajavafxjavafx-8

解决方案


推荐阅读