为了防止有人想帮助我,我将非常感激,因为我在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”,并且每次用户切换到组合框时切换它?但在我看来和“硬编码”方法。
你会怎么做?