【问题标题】:How to manipulate Object through the selectedItem of a Combobox如何通过组合框的 selectedItem 操作对象
【发布时间】: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”并在每次用户将项目更改为组合框时切换它?但在我看来和“硬编码”方法有关。

你会怎么做?

【问题讨论】:

    标签: java javafx combobox


    【解决方案1】:

    嗯,没有一种设计应用程序的正确方法。当然,有一些设计原则,你应该尽量遵循。关于该主题的信息很多,因此找到它不会有问题。
    关于您的具体情况,我会将组合框和Person 选择逻辑从EventiLayoutController 移动到RootLayoutController。这样您就可以在根控制器中选择Person 实例,并且可以在发生更改时通知事件控制器并向其提供选定的Person。通知可以通过在RootLayoutController 中直接使用EventiLayoutController 实例或使用事件总线方法来完成。
    另外,我将使用属性绑定(或双向绑定,取决于您的逻辑)而不是 mainApp.getEventi().addAll(newValue.getEventi());。但我真的不太明白为什么你需要在 MainApp 中单独的 eventi 集合,如果你无论如何都要用选定的人事件填充它。

    【讨论】:

      猜你喜欢
      • 2010-10-06
      • 1970-01-01
      • 2013-03-01
      • 2014-09-06
      • 2016-06-09
      • 2011-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多