JavaFX-从另一个控制器获取数据的组合框更新

JavaFX-从另一个控制器获取数据的组合框更新,java,combobox,javafx,Java,Combobox,Javafx,这是我的小问题: @FXML private ComboBox<Person> personcb; private ObservableList<Person> persons = FXCollections.observableArrayList(); private ResourceBundle langBundle; @Override public void start(Stage primaryStage) { try { this

这是我的小问题:

@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){
        }
    }
loginwithsec
是我的
第一个控制器上定义的
@FXML
文档按钮

概览加载器
是第二个控制器的FXMLLoader

现在,用户应在
第一个控制器
管理窗格的组合框上选择一个


概述(第二个控制器)
通过某些DAO的AFAIK休眠加载当前选择的数据。因此,延迟加载的DAO必须自动填入
组合框
,以向用户显示一些人是可用的
  • 父控制器
  • 儿童控制器
ParentController
有一个
observeListParentList
,用作组合框中的项目

public class ParentController {
    ...
    private ObservableList parentList = FXCollections.observableArrayList();
    ...
}
ChildController
有一个
observblelistchildlist
,它是使用hibernate(或其他方式)从数据库中填充的。我们需要将此数据传递回
父列表
,以填充其数据

public class ChildController {
    ...
    private ObservableList childList = FXCollections.observableArrayList();
    ...
}
我们通过公共方法公开
childList

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

    public ObservableList getChildList() {
        return childList;
    }
    // Some logic to load the list    
    ...
}
回到
ParentController
,因为我们使用的是
observateList
而不是
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());
    }
    ...
}

当您在组合框中选择一个项目时,是否正在调用
persons()
?目前是,但这是一种糟糕的方式,因为它仅通过选择一个项目来更新列表。如果我通过另一个控制器将项目添加到列表中,我希望有一种方法可以自动更新它们。为什么不从
initialize()
调用该方法呢。这样,在加载fxml.mhm时,组合框项将被更新,这也确实有效,但cb while init的数据不是必需的。通过事件调用person是我尝试通过添加一些testperson来更新自己,但它只有在单击某些选择时才起作用。我不确定你在问什么。你能解释清楚吗?
public class ChildController {
    ...
    private ObservableList childList = FXCollections.observableArrayList();

    public ObservableList getChildList() {
        return childList;
    }
    // Some logic to load the list    
    ...
}
public class ParentController {
    ...
    @FXML
    ComboBox comboBox;  

    private ObservableList parentList = FXCollections.observableArrayList();

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