【问题标题】:How add a column data in other modifying its values by an operation如何在其他通过操作修改其值时添加列数据
【发布时间】:2016-03-28 09:51:18
【问题描述】:

如何将 TableView 的一列的数据添加到同一个表的另一列但进行 x 操作(如添加 5)?

就我而言,我想将 inTaxColumn * 0.79 的数据添加到 outTaxColumn。

这里是控制器

//Imports

public class ControladorView implements Initializable {

    @FXML private TableView tableViewBudget;
    @FXML private TableColumn<Product, String> nameBudgetColumn;
    @FXML private TableColumn<Product, Double> outTaxColumn;
    @FXML private TableColumn<Product, Double> inTaxColumn;
    @FXML private TableColumn<Product, Integer> quantityColumn;

    private ObservableList<Product> budgetData;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

        //Budget Table
        nameBudgetColumn.setCellValueFactory(
            new PropertyValueFactory<>("description"));
        inTaxColumn.setCellValueFactory(
            new PropertyValueFactory<>("price"));

        budgetData = FXCollections.observableArrayList();
        tableViewBudget.setItems(budgetData);
}

产品类别:

public class Product {
    public enum Category {
        SPEAKER, HDD, HDD_SSD, POWER_SUPPLY, DVD_WRITER, RAM, SCREEN,
        MULTIREADER, MOTHERBOARD, CPU, MOUSE, GPU, KEYBOARD, CASE, FAN
    }

    public Product(String description, double price, int stock, Category category) {
        this.description = description;
        this.price = price;
        this.stock = stock;
        this.category = category;
    }

    public Category getCategory() {
        return category;
    }

    public String getDescription() {
        return description;
    }

    public double getPrice() {
        return price;
    }

    public int getStock() {
        return stock;
    }

    private final String description;
    private final double price;
    private final int stock;
    private final Category category;
}

【问题讨论】:

  • 请出示你的Product班级
  • @James_D 我已添加 :)

标签: java javafx tableview javafx-8


【解决方案1】:

你可以的

outTaxColumn.setCellValueFactory(cellData ->
    new SimpleDoubleProperty(cellData.getValue().getPrice() * 0.79 ).asObject());

【讨论】:

  • 你能解释一下代码是如何工作的吗?因为我不明白如何知道 TableColumn 必须从中获取值的 lambda 表达式。
  • 单元格值工厂将行的对象映射到要在单元格中显示的可观察值。 cellData.getValue() 为您提供该行的对象(Product)。 cellData.getValue().getPrice() 为您提供 Product 的价格,然后乘以 0.79。由于您将该列声明为 TableColumn&lt;Product, Double&gt;,因此您需要和 ObservableValue&lt;Double&gt;:将值包装在 SimpleDoubleProperty 中会给您一个 ObservableValue&lt;Number&gt;asObject() 给出正确的类型。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-09
  • 1970-01-01
  • 1970-01-01
  • 2013-06-22
相关资源
最近更新 更多