根据您的要求,可以有不同的方法。可以是:
@Override
public void start(Stage stage)
{
final Map<String, ObservableList<Integer>> map = new HashMap<>();
map.put("cupcakes", FXCollections.observableArrayList(1,2,3,4,5,6));
map.put("cookies", FXCollections.observableArrayList(11,12,13,14,15,16));
final ComboBox<String> comboOne = new ComboBox<>();
comboOne.getItems().addAll(
"cupcakes",
"cookies",
"empty box"
);
final ComboBox<Integer> comboTwo = new ComboBox<>();
comboOne.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>()
{
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue)
{
comboTwo.setItems(
map.containsKey(newValue) ? map.get(newValue) : FXCollections.emptyObservableList()
);
}
});
VBox grid = new VBox(20);
grid.getChildren().addAll(comboOne, comboTwo);
Scene scene = new Scene(grid, 450, 250);
stage.setScene(scene);
stage.show();
}
您是否具有确定性功能,将Box 1的内容映射到Box 2的内容? – ItachiUchiha 2015-04-03 14:05:25
你有你试过的代码吗? – Alupotha 2015-04-03 14:16:57
不知道我明白你的意思,但我所尝试的是通过使用if/else来更改Box 2的项目,但是它不成功。 – spacedout 2015-04-03 14:25:11