在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被引用调用。对方法参数的任何赋值仅对方法内部有效。而且,据我所知,您确实创建了想要早些[[modify