JavaFX - ComboBox更新从另一个控制器获取数据

JavaFX - ComboBox update fetching data from another controller

这是我的小问题:

@FXML 
private ComboBox<Person> personcb;
private ObservableList<Person> persons = FXCollections.observableArrayList();

private ResourceBundle langBundle;

@Override
public void start(Stage primaryStage) {
    try {
        this.Stage = primaryStage;
        this.Stage.initStyle(StageStyle.UNDECORATED);
        rootLayout = initRootLayout(Locale.getDefault());
        Scene scene = new Scene(rootLayout);
        scene.getStylesheets().add(getClass().getResource("any.css").toExternalForm());
        Stage.setScene(scene);
        Stage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

@Override
public void initialize(URL location, ResourceBundle resources) {
    langBundle = resources;
    lblTextByController.setText(langBundle.getString("key1"));
    personcb.valueProperty().addListener(new ChangeListener<Person>() {

        @Override
        public void changed(ObservableValue<? extends Person> observable,
                Person oldValue, Person newValue) {
            System.out.println("value updated");
        }
    });
}

@FXML
    private void persons() {
        try{
            if(personcb.getItems() == null || personcb.getItems().size() != secondController.getUSER().getPersons().size()){
                ObservableList<Person> persons= 
                        FXCollections.observableArrayList();
                options.addAll(secondController.getUSER().getPersons());
                personcb.setItems(persons);
                personcb.setPromptText(langBundle.getString("key402") +" [" + personcb.size()+"]");
            }
        }
        catch(NullPointerException e){
        }
    }

在我的 FXML 文件中,组合框定义为:

<ComboBox fx:id="personcb" onShowing="#persons" prefWidth="200.0" promptText="%key402">

我想在我的 第一个控制器 initialize() 之后从我的第二个控制器 添加项目。

只有当我 select 我的 ComboBox 中的一个项目更新其他项目时,我当前的解决方案才有效。


编辑 - 更多信息

我的第一个控制器管理根窗格并包含作为根的 BorderPane 和一些 buttons 登录和 ComboBox。用户登录完成后,我的第二个控制器由以下代码示例加载。

AnchorPane barHolder = (AnchorPane) overviewLoader.load(); 
BorderPane root = (BorderPane) loginwithsec.getScene().getRoot();
root.setCenter(barHolder); 

loginwithsec 是在我的 first controller.

文档中定义的 @FXML 按钮

overviewLoader 是第二个控制器的 FXMLLoader。

现在,用户应 select Personfirst Controller 托管窗格的 ComboBox 上。

一些 DAO 的 AFAIK Hibernate 对当前 selections 的概述(第二个控制器)load the data。因此延迟加载的 DAO 必须在 ComboBox 中自动填充以向用户显示某些 Persons 可用。

让我们假设您有两个控制器并调用它们

  • 父控制器
  • 子控制器

ParentController 有一个 ObservableList parentList 用作 ComboBox 中的项目。

public class ParentController {
    ...
    private ObservableList parentList = FXCollections.observableArrayList();
    ...
}

ChildController 有一个 ObservbleList childList,它是使用休眠(或其他)从数据库中填充的。我们需要将此数据传回 parentList 以填充其数据。

public class ChildController {
    ...
    private ObservableList childList = FXCollections.observableArrayList();
    ...
}

我们通过 public 方法公开 childList

public class ChildController {
    ...
    private ObservableList childList = FXCollections.observableArrayList();

    public ObservableList getChildList() {
        return childList;
    }
    // Some logic to load the list    
    ...
}

回到ParentController,因为我们使用ObservableList而不是List,我们可以利用并绑定父控制器和子控制器中的列表数据。 Bindings.bindContentBidirectional() 确保任何一个列表中的任何更改都将反映在另一个列表中。

public class ParentController {
    ...
    @FXML
    ComboBox comboBox;  

    private ObservableList parentList = FXCollections.observableArrayList();

    public void initialize() {
       comboBox.setItems(parentList);
       Bindings.bindContentBidirectional(parentList, childController.getChildList());
    }
    ...
}