爲了防止有人想幫助我,我將非常感激,因爲我在java中嘗試學習簡單概念,並且在同一時間保持代碼清潔並簡單閱讀。如果你認爲這是一個愚蠢的問題,因爲我有錯誤的編碼方法,我會真正關心你的意見,如果你解釋我爲什麼我會嘗試獲得你的經驗。如何通過組合框的SelectedItem操作對象
現在的問題是:
我有個人的含observableList和observableList事件mainApp
public class MainApp extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
private ObservableList<Evento> eventi = FXCollections.observableArrayList();
private ObservableList<Person> persons = FXCollections.observableArrayList();
public ObservableList<Evento> getEventi() {
return eventi;
}
public ObservableList<Person> getPersons() {
return persons;
}
public static void main(String[] args) {launch(args);}
public void start(Stage primaryStage){
this.primaryStage = primaryStage;
this.primaryStage.setTitle("Bettator Events");
Person test = new Person("Daniele", "Giaquinto");
test.getEventi().add(new Evento("danEvento", 1.0, "testConto"));
persons.add(test);
Person test2 = new Person("Roberto", "Sellitto");
test2.getEventi().add(new Evento("robEvento", 31.0, "testCo"));
persons.add(test2);
initRootLayout();
showEventiLayout();
}
Person對象有一個事件列表也一樣,我創造了它之後,因爲我正在試圖創建一個響應式GUI,所以在這種情況下,而不是一個事件列表,每個人都有他們的事件列表。
public class Person {
private StringProperty firstName;
private StringProperty lastName;
private StringProperty nickName;
private ObservableList<Evento> eventi = FXCollections.observableArrayList();
我創建到EventyLayout組合框和的tableView,當我改變了組合框,使用監聽器的的tableView將與人對象的事件列表來填充,我這樣做填充mainApp事件列表與選定人員事件列表
(EventiController)
public class EventiLayoutController {
private MainApp mainApp;
@FXML
private ComboBox<Person> utenteCmb;
@FXML
private TableView<Evento> eventiTbl;
@FXML
private TableColumn<Evento, String> eventoCol;
@FXML
private TableColumn<Evento, Double> importoCol;
@FXML
private TableColumn<Evento, String> contoCol;
@FXML
void initialize() {
eventoCol.setCellValueFactory(cellData -> cellData.getValue().nomeProperty());
importoCol.setCellValueFactory(cellData -> cellData.getValue().importoProperty().asObject());
contoCol.setCellValueFactory(cellData -> cellData.getValue().contoProperty());
utenteCmb.setCellFactory((comboBox) -> new ListCell<Person>() {
//cut for stackoverflow
});
utenteCmb.setConverter(new StringConverter<Person>() {
//cut for stackoverflow
});
utenteCmb.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
mainApp.getEventi().clear();
mainApp.getEventi().addAll(newValue.getEventi());
});
}
public void setMainApp(MainApp mainApp){
this.mainApp = mainApp;
eventiTbl.setItems(mainApp.getEventi());
utenteCmb.setItems(mainApp.getPersons());
}
}
的項目是沒有possibilit前y以切換用戶,我創建了一個「handleAddEvent」添加事件到mainApp名單,在rootLayoutController(就是那個告訴我的菜單欄和菜單項,其中添加按鈕)
public class RootLayoutController {
private MainApp mainApp;
@FXML // ResourceBundle that was given to the FXMLLoader
private ResourceBundle resources;
@FXML // URL location of the FXML file that was given to the FXMLLoader
private URL location;
@FXML // This method is called by the FXMLLoader when initialization is complete
void initialize() {
}
/**
* Needed for pass the MainApp to this controller ad use the public variable
*/
public void setMainApp(MainApp mainApp){
this.mainApp = mainApp;
}
/**
* Open dialog for add a new event, then add it to the event collection
*/
@FXML
private void handleAdd() {
Evento evento = new Evento();
boolean okClicked = mainApp.showEventiEditEditDialog(evento);
if (okClicked)
mainApp.getEventi().add(evento);
}
}
後來我試圖深入學習,我需要將該事件直接添加到person-> event中,如果我嘗試使用相同的函數添加它,他會將其添加到mainApp事件列表中,而不是添加到person事件列表中即使mainApp人員列表中充滿了人事件。
我想知道是否有一種方法將observableList傳遞給mainApp,或者有一種簡單的方法可以實現我的目標。
當他嘗試添加事件時,RootLayoutController不知道有一個組合框,並且mainApp也不必知道存在組合框,所以如何獲取selectedPerson並向其添加事件事件列表?
我應該在mainApp中創建一個「activePerson」,並且每次用戶切換到組合框時切換它?但在我看來和「硬編碼」方法。
你會怎麼做?