JavaFX静态ObservableList未刷新组合框

JavaFX静态ObservableList未刷新组合框,javafx,combobox,observablelist,Javafx,Combobox,Observablelist,我想做的是创建一个类来维护国家的静态可观察列表。我想在组合框中显示这些国家。我把这部分做好了。现在,我还想使用户能够将新的国家/地区添加到列表中。因此,组合框旁边有一个按钮,显示另一个对话框,允许输入另一个国家名称。在用户输入国家名称并单击save之后,我希望使用新的国家更新单个静态ObservableList,然后它显示在组合框中。这部分没有发生 我将展示哪些有效,哪些无效 保存对静态列表的引用并进行有效的更新。像这样: public class CustomerController impl

我想做的是创建一个类来维护国家的静态可观察列表。我想在组合框中显示这些国家。我把这部分做好了。现在,我还想使用户能够将新的国家/地区添加到列表中。因此,组合框旁边有一个按钮,显示另一个对话框,允许输入另一个国家名称。在用户输入国家名称并单击save之后,我希望使用新的国家更新单个静态ObservableList,然后它显示在组合框中。这部分没有发生

我将展示哪些有效,哪些无效

保存对静态列表的引用并进行有效的更新。像这样:

public class CustomerController implements Initializable {

    private ObservableList<Country> countryList;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        countryList = Country.getCountryList();
        comboCountry.setItems(countryList);
    }

    ...

    // Fired when clicking the "new country" button
    @FXML
    void handleNewCountry(ActionEvent event) {
        Country country = new Country();
        country.setCountry("Austria");
        countryList.add(country);
    }
}

因此,我的理论是,当调用setItems时,ComboBox会以某种方式创建ObservableList的新实例。不过我真的不确定。静态对象应该只有一个实例,因此从任何地方更新它都应该更新该组合框。有人知道这是怎么回事吗?

每次调用
Country
构造函数时,您都在创建一个新的
observeList
实例。这样就修改了与组合框使用的列表不同的列表

如果您确实需要将国家/地区列表保存在静态字段中(这被认为是错误的做法),则应确保只创建一个
可观察列表

private static final ObservableList<Country> countryList = FXCollections.observableArrayList();
private static final ObservableList countryList=FXCollections.observableAryList();

(也从构造函数中删除此字段的赋值。)

我知道这是一个小问题。谢谢你指出!尽管我确实想知道为什么将这样一个列表保存在一个静态列表中会被认为是不好的做法。“我的总体想法是,当涉及到国家名单时,要有一个单一的真相来源
public class Country extends BaseModel {
    private int countryID;
    private StringProperty country;
    private static ObservableList<Country> countryList; // The static observable list

    public Country() {
        countryList = FXCollections.observableArrayList();
        country = new SimpleStringProperty();
    }

    public int getCountryID() {
        return countryID;
    }

    public void setCountryID(int countryID) {
        this.countryID = countryID;
    }

    public StringProperty countryProperty() {
        return this.country;
    }

    public String getCountry() {
        return this.country.get();
    }

    public void setCountry(String country) {
        this.country.set(country);
    }

    public boolean equals(Country country) {
        if (this.getCountry().compareToIgnoreCase(country.getCountry()) != 0) {
            return false;
        }
        return true;
    }

    public static ObservableList<Country> getCountryList() {
        if (countryList.size() < 1) {
            updateCountryList();
        }
        return countryList;
    }

    public static void updateCountryList() {
        countryList.clear();
        ArrayList<Country> daoList = CountryDao.listCountries();
        for (Country country : daoList) {
            countryList.add(country);
        }
    }

    @Override
    public String toString()  {
        return this.getCountry();
    }
}
public class CountryController implements Initializable {
    @FXML
    private TextField textCountry;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
    }    

    @FXML
    void handleSave(ActionEvent event) {
        Country country = new Country();
        country.setCountry(textCountry.getText().trim());
        CountryDao.insert(country); // Insert the country into the database
        Country.updateCountryList(); // Update the static ObservableList
        close();
    }

    @FXML
    void handleCancel() {
        close();
    }

    void close() {
        final Stage stage = (Stage) textCountry.getScene().getWindow();
        stage.close();        
    }
}
private static final ObservableList<Country> countryList = FXCollections.observableArrayList();