2016-06-20 40 views
0

我想将数量列绑定到价格和数量列,以便每次数量或价格,金额更新。绑定列以乘以其他列

import javafx.application.Application; 
    import javafx.collections.FXCollections; 
    import javafx.collections.ObservableList; 
    import javafx.event.EventHandler; 
    import javafx.geometry.Insets; 
    import javafx.scene.Group; 
    import javafx.scene.Scene; 
    import javafx.scene.control.Label; 
    import javafx.scene.control.TableColumn; 
    import javafx.scene.control.TableColumn.CellEditEvent; 
    import javafx.scene.control.TableView; 
    import javafx.scene.control.cell.PropertyValueFactory; 
    import javafx.scene.control.cell.TextFieldTableCell; 
    import javafx.scene.layout.HBox; 
    import javafx.scene.layout.VBox; 
    import javafx.scene.text.Font; 
    import javafx.stage.Stage; 
    import javafx.util.converter.NumberStringConverter; 

    /** 
    * 
    * @author Yunus 
    */ 
public class ColumnBinding extends Application{ 
    private TableView<Product> table = new TableView<Product>(); 
    private final ObservableList<Product> data = FXCollections.observableArrayList(); 
    final HBox hb = new HBox(); 

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

@Override 
public void start(Stage primaryStage) throws Exception { 
    Scene scene = new Scene(new Group()); 
    primaryStage.setTitle("Book Store Sample"); 
    primaryStage.setWidth(650); 
    primaryStage.setHeight(550); 

    final Label label = new Label("Testing"); 
    label.setFont(new Font("Arial", 20)); 

    table.setEditable(true); 

    TableColumn priceCol = new TableColumn("Price"); 
    priceCol.setMinWidth(100); 
    priceCol.setCellValueFactory(
      new PropertyValueFactory<Product, String>("price")); 
    priceCol.setCellFactory(TextFieldTableCell.<Product, Number>forTableColumn(new NumberStringConverter())); 
    priceCol.setOnEditCommit(
      new EventHandler<CellEditEvent<Product, Number>>() { 
       @Override 
       public void handle(CellEditEvent<Product, Number> t) { 
        ((Product) t.getTableView().getItems().get(
          t.getTablePosition().getRow()) 
        ).setPrice(t.getNewValue().intValue()); 
       } 
      } 
    ); 

    TableColumn quantityCol = new TableColumn("Quantity"); 
    quantityCol.setMinWidth(200); 
    quantityCol.setCellValueFactory(
      new PropertyValueFactory<Product, Number>("quantity")); 
    quantityCol.setCellFactory(TextFieldTableCell.<Product, Number>forTableColumn(new NumberStringConverter())); 
    quantityCol.setOnEditCommit(
      new EventHandler<CellEditEvent<Product, Number>>() { 
       @Override 
       public void handle(CellEditEvent<Product, Number> t) { 
        ((Product) t.getTableView().getItems().get(
          t.getTablePosition().getRow()) 
        ).setQuantity(t.getNewValue().intValue()); 
       } 
      } 
    ); 

    TableColumn amount = new TableColumn("Amount"); 
    amount.setMinWidth(200); 
    amount.setCellValueFactory(
      new PropertyValueFactory<Product, String>("amount")); 

    data.addAll(new Product(10, 12, 120), 
       new Product(20, 12, 240), 
       new Product(30, 12, 360), 
       new Product(40, 12, 480), 
       new Product(50, 12, 600)); 
    table.setItems(data); 
    table.getColumns().addAll(priceCol, quantityCol, amount); 
    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); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 

public static class Product{ 
    Product(){} 

    public Product(float quantity, float price, float amount) { 
     this.quantity = quantity; 
     this.price = price; 
     this.amount = amount; 
    } 

    private float quantity; 
    private float price; 
    private float amount; 

    public float getQuantity() { 
     return quantity; 
    } 

    public void setQuantity(float quantity) { 
     this.quantity = quantity; 
    } 

    public float getPrice() { 
     return price; 
    } 

    public void setPrice(float price) { 
     this.price = price; 
    } 

    public float getAmount() { 
     return amount; 
    } 

    public void setAmount(float amount) { 
     this.amount = amount; 
    } 


} 
} 

所面临的挑战,虽然是不改变POJO类(产品),属性字段

+1

“所面临的挑战,虽然是不改变POJO类属性字段”。为什么?为什么限制自己使用专门为您打算的目的而提供的API? –

+0

因为我将不得不改变许多其他地方的源代码我猜,因为我使用构造函数没有参数和属性值需要初始化在构造函数 –

+0

为什么你需要改变任何东西?唯一的公共方法仍然存在,并且仍然具有完全相同的功能(这就是JavaFX Property模式的设计点)。 –

回答

2

我只想在模型类中使用JavaFX的属性。然后,你可以通过在模型结合建立的关系:

public static class Product{ 

    private final FloatProperty quantity = new SimpleFloatProperty(); 
    private final FloatProperty price = new SimpleFloatProperty(); 
    private final ReadOnlyFloatWrapper amount = new ReadOnlyFloatWrapper(); 

    Product(){ 
     this(0f, 0f); 
    } 

    // if amount is supposed to depend on quantity and price, it makes 
    // no sense at all to have a constructor taking parameters for all 
    // three values... 
    public Product(float quantity, float price) { 
     setQuantity(quantity); 
     setPrice(price); 
     this.amount.bind(this.quantity.multiply(this.price)); 
    } 

    public float getQuantity() { 
     return quantityProperty().get(); 
    } 

    public void setQuantity(float quantity) { 
     quantityProperty().set(quantity); 
    } 

    public FloatProperty quantityProperty() { 
     return quantity ; 
    } 

    public float getPrice() { 
     return priceProperty().get(); 
    } 

    public void setPrice(float price) { 
     priceProperty().set(price); 
    } 

    public FloatProperty priceProperty() { 
     return price ; 
    } 

    public float getAmount() { 
     return amountProperty.get(); 
    } 

    // Again, it makes no sense at all to have this method 
    // if amount depends on the other values 
    // public void setAmount(float amount) { 
    //  this.amount = amount; 
    // } 

    public ReadOnlyFloatProperty amountProperty() { 
     return amount.getReadOnlyProperty(); 
    } 

} 

现在你的表列很简单:

TableColumn<Product, Float> priceColumn = new TableColumn<>("Price"); 
priceColumn.setCellValueFactory(cellData -> cellData.getValue().priceProperty().asObject()); 

TableColumn<Product, Float> quantityColumn = new TableColumn<>("Quantity"); 
quantityColumn.setCellValueFactory(cellData -> cellData.getValue().quantityProperty().asObject()); 

TableColumn<Product, Float> amountColumn = new TableColumn<>("Amount"); 
amountColumn.setCellValueFactory(cellData -> cellData.getValue().amountProperty().asObject()); 
+0

你真棒。 –