2016-03-28 65 views
2

如何将TableView的一列的数据添加到其他同一个表中,但执行x操作(如添加5)?如何在其他修改其值的操作中添加列数据

在我的情况下,我想添加到outTaxColumn inTaxColumn * 0.79的数据。

这里是控制器

//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; 
} 
+0

请出示你的'Product'类 –

+0

@James_D我加入了:) –

回答

2

你可以做

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

你能解释一下代码的工作原理吗?因为我不明白如何知道TableColumn必须采用这些值的lambda表达式。 –

+1

单元格值工厂将该行的对象映射到要显示在单元格中的可观察值。 'cellData.getValue()'为您提供行的对象('Product')。 'cellData.getValue()。getPrice()'为您提供该产品的价格,然后乘以0.79。由于您将该列声明为一个'TableColumn ',您需要和'ObservableValue ':将值包装在'SimpleDoubleProperty'中给出一个'ObservableValue '和['asObject()'](http: /docs.oracle.com/javase/8/javafx/api/javafx/beans/property/DoubleProperty.html#asObject--)给出正确的类型。 –

相关问题