它是一個管理學生的應用程序,它有一個名爲StudentsViewController
的類,它將圖形部分(StudentView
)與有效地對ArrayList
(Service
)進行更改的功能鏈接起來。我會告訴你我不JavaFX的圖形用戶界面瞭解部分:初學者JavaFX觀察者
public class StudentViewController implements Observer<Student>{
private ObservableList<Student> model;
private StudentView view;
StudentService service;
public StudentViewController(StudentService service, StudentView view){
this.view=view;
this.model= FXCollections.observableArrayList(service.getAllStudents());
view.studTable.setItems(model);
this.service=service;
}
@Override
public void update(Observable<Student> observable) {
StudentService s=(StudentService)observable;
model.setAll(s.getAllStudents());
}
}
我的問題是:
如果我有一個ObservableList
是圍繞我ArrayList
和使用該ObservableList
一個TableView
,爲什麼我需要更新功能?
爲什麼我必須清除模型中的所有數據並在其中放置一個新的數據?
JavaFX甚至沒有'Observer'類。如果沒有關於目的和目標是什麼的進一步信息,很難說這裏正在努力完成什麼。即使有更多的信息,我懷疑這將是一個看似難以回答的問題。 – jewelsea
對於JavaFX設計的一個很好的介紹(其中包括一個由類似於您的目標的模型支持的人員數據表視圖),我建議查看[makery JavaFX教程](http://code.makery.ch/library/JavaFX的8-教程/)。在試圖回答這個「如何完成」這個問題時,我發現我只是模仿了教程中使用的編程模式的本質。 – jewelsea
爲了更好地理解觀察者在GUI體系結構中的使用,請參見:[Martin Fowler的GUI體系結構](http://martinfowler.com/eaaDev/uiArchs.html)。請注意,JavaFX擁有大量的內置控件和類來處理observable屬性,所以這些通常直接使用,而不是像在一些GUI架構示例和您的代碼中那樣實現自己的'Observer'樣式代碼示例問題。 – jewelsea