2016-06-20 8 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; 
    } 


} 
} 

도전하지만 난 그냥 모델 클래스에서 자바 FX 속성을 사용 특성 필드

+1

"도전은 비록 속성 필드 POJO 클래스를 변경하지 않는 것입니다." 왜? 귀하가 의도 한 목적을 위해 특별히 제공된 API를 사용하지 못하도록 제한하는 이유는 무엇입니까? –

+0

소스 코드에서 다른 많은 부분을 변경해야 할 것입니다. 매개 변수없이 생성자를 사용하고 속성 값을 생성자에서 초기화해야합니다. –

+0

왜 다른 것을 변경해야합니까? 유일하게 공개되는 메소드는 여전히 존재할 것이고 똑같은 기능 (JavaFX Property 패턴이 설계된 방식의 일종)이 될 것이다. –

답변

2

와 POJO 클래스 (제품)을 변경하지 않는 것입니다. 그럼 당신은 모델에 바인딩하여 관계를 설정 할 수 있습니다

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

당신은 최고입니다. –