具有TextFields和TableView的Issu

时间:2019-03-26 16:08:24

标签: java javafx javafx-8

你好(回到之前帮助过我的人)

我在代码中提出了一个新问题。因此,在此之前,我有一个issu,可以在tableview上添加我的combobox值,并且可以成功修复它。但是现在,我无法在表中添加文本字段“ text”(以前不是问题),那么您可以第二次帮我吗? (因此我是法语,首先,对我的英语感到抱歉,其次,我尝试将所有论点和其他论点翻译成英语,以便更好地理解。)

我为您链接了实际结果的图片: 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);
    }
}
}

0 个答案:

没有答案