从动态创建的另一个ComboBox更新在Javafx中动态创建的ComboBox的内容

问题描述 投票:-1回答:1

GridPane中,我正在动态创建两个ComboBox。对于第一个ComboBox,我在加载场景时收费。然后,我希望当我在此ComboBox中执行操作时,其他ComboBox的项目根据所选的值加载。

ComboBox<String> combobox1 = loadItems();
ComboBox<String> combobox2 = new ComboBox<String>();

gridpane.add(combobox1, 0, 0);
gridpane.add(combobox2, 1, 0);

我已经尝试过使用listener,但似乎没有用:

combobox1.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
    @Override
    public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
            loadList(combobox2, newValue);
                }
            }); 

private void loadList(ComboBox<String> combobox, String value) {
        combobox = getCorrespondingList(value);
    }

public ComboBox<String> getCorrespondingList(String value) {
        ComboBox<String> combobox = new ComboBox<String>();
        ArrayList<String> list = new ArrayList<String>();
        try {
            String query = "select ... where Item = '" + value 
                    + "' order by c";
            statement = connection.prepareStatement(query);
            result = statement.executeQuery();
            while (result.next()) {
                list.add(result.getString(1));
            }
        }
        catch (SQLException e) {
            e.getMessage();
        }
        ObservableList<String> observableList = FXCollections.observableArrayList(list);
        combobox.setItems(observableList);
        return combobox;
    }

我非常感谢您的帮助。

java javafx dynamic combobox listener
1个回答
0
投票

Java被引用调用。对方法参数的任何赋值仅对方法内部有效。而且,据我所知,您确实创建了想要早些[[modify

© www.soinside.com 2019 - 2024. All rights reserved.