2017-07-29 58 views
2

如何檢測數據的變化(不僅添加/移除,而且改變對象的屬性)由ObservableList包含具有這樣的特性的對象填入的tableView:如何檢測的tableView了JavaFx數據變化

public class Car{ 
private SimpleIntegerProperty id = new SimpleIntegerProperty(); 
private SimpleStringProperty brand = new SimpleStringProperty(); 
private SimpleStringProperty engine = new SimpleStringProperty(); 
private SimpleBooleanProperty navi = new SimpleBooleanProperty(); 
private SimpleBooleanProperty available = new SimpleBooleanProperty(); 
private SimpleDoubleProperty liters = new SimpleDoubleProperty(); 
private SimpleIntegerProperty power = new SimpleIntegerProperty(); 

(...) 
} 

在控制器類我這是怎麼結合在一起:

class MainController 
{ 

@FXML 
public void initialize() 
{ 

    idColumn.setCellValueFactory(new PropertyValueFactory<Car, Integer>("id")); 
    engineColumn.setCellValueFactory(new PropertyValueFactory<Car, String>("engine")); 
    brandColumn.setCellValueFactory(new PropertyValueFactory<Car, String>("brand")); 
    naviColumn.setCellValueFactory(new PropertyValueFactory<Car, Boolean>("navi")); 
    availableColumn.setCellValueFactory(new PropertyValueFactory<Car, Boolean>("available")); 
    powerColumn.setCellValueFactory(new PropertyValueFactory<Car, Integer>("power")); 
    fuelColumn.setCellValueFactory(new PropertyValueFactory<Car, Double>("liters")); 
    carModel.getObservableList().add(new Car(213,"FIAT","1.9 JTD",true,true,32.4,132)); 
    tableView.setItems(carModel.getObservableList()); 
} 

我嘗試添加到初始化observableList方法偵聽器,但很遺憾,只能檢測添加和從列表中刪除的對象。我想聽衆更新的tableView每當從對象的任何屬性將被改變

carModel.getObservableList().addListener((Change<? extends Car> c) -> { 
     while (c.next()) { 
      if (c.wasAdded()) { 
       System.out.println("Added:"); 
       c.getAddedSubList().forEach(System.out::println); 
       System.out.println(); 
      } 
      if (c.wasRemoved()) { 
       System.out.println("Removed:"); 
       c.getRemoved().forEach(System.out::println); 
       System.out.println(); 
      } 
      if (c.wasUpdated()) { 
       System.out.println("Updated:"); 
       carModel.getObservableList().subList(c.getFrom(), c.getTo()).forEach(System.out::println); 
       System.out.println(); 
      } 
     } 
     }); 

我發現論壇等sollution這正如我說如果你只需要改變一個事實只檢測添加/刪除對象

+2

[以正確的方式構建列表](https://docs.oracle.com/javase/8/javafx/api/javafx/collec tions/FXCollections.html#observableArrayList-javafx.util.Callback-)或手動添加/刪除偵聽器 - 您的選擇......或者當您修改屬性時只需執行所需的代碼......(可以使用「TableColumn.onEditCommit」對於可編輯的列......) – fabian

+1

「我希望偵聽器在來自對象的任何屬性將被更改時更新tableView」。假設您已在[常規模式](http://www.oracle.com/pls/topic/lookup?ctx=javase80&id=JFXBD107)之後的'Car'中定義了方法,當屬性更改時,表視圖將自動更新。不需要使用提取器或定義其他偵聽器,['PropertyValueFactory'](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/cell/PropertyValueFactory.html)爲你做的工作。 –

回答

1

屬性最簡單的方法是將所有屬性連接成一個ObservableValue

在你Car類有這樣的方法:

ObservableValue changeProperty() { 
    return Bindings.concat(id, brand, engine, navi, available, liters, power); 
} 

在客戶端,你只是聽吧:

car.changeProperty().addListener(new ChangeListener() { 
     @Override 
     void changed(ObservableValue observable, Object oldValue, Object newValue) { 
      // one of property was changed, do something 
     } 
    }) 

用同樣的方法,如果你需要的所有汽車中檢測到的變化:

ObservableValue changeProperty() { 
    return Bindings.concat(car1.changeProperty(), car2.changeProperty(), carN.changeProperty()); 
}