【发布时间】:2016-07-15 20:35:40
【问题描述】:
以防万一有人想帮助我,我会非常感激,因为我是 Java 的菜鸟,并试图学习简单的概念,同时保持代码的简洁和易于阅读。如果您认为这是一个愚蠢的问题,因为我的编码方法有误,我会非常关心您的意见,如果您解释一下为什么我会尝试获取您的经验。
现在的问题是:
我有一个 mainApp,其中包含 observableList of people 和 observableList of events
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 事件列表活动列表
(事件控制器)
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());
}
}
在项目无法切换用户之前,我创建了一个“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 事件列表中并不在人员事件列表中,即使 mainApp 人员列表中填充了人员事件。
我想知道是否有办法将 observableList 作为指针传递给 mainApp,或者有一种简单的方法可以达到我的目标。
RootLayoutController 尝试添加事件时并不知道有一个组合框,而 mainApp 也不必知道有一个组合框,那么如何获取 selectedPerson 并在他的事件中添加一个事件列表?
我是否应该在 mainApp 中创建一个“activePerson”并在每次用户将项目更改为组合框时切换它?但在我看来和“硬编码”方法有关。
你会怎么做?
【问题讨论】: