我是javafx的新手。我想使用Combox更改ListView的选择模型
有我的代码:
private String[] selectionMode = {"SINGLE","MULTIPLE"};
private ComboBox<String> cboSelectionMode = new ComboBox<>();
private String[] items = {"America","British","China","Denmark","Germany",
"Japan","Korea"};
@Override
public void start(Stage primaryStage) {
BorderPane paneForSelection = new BorderPane();
paneForSelection.setLeft(new Label("Choose Selection Mode: "));
paneForSelection.setCenter(cboSelectionMode);
cboSelectionMode.setPrefWidth(125);
cboSelectionMode.setValue("SINGLE");
ListView<String> lvItems = new ListView<>(FXCollections.observableArrayList(items));
FlowPane paneForText = new FlowPane();
ObservableList<String> selection = FXCollections.observableArrayList(selectionMode);
cboSelectionMode.getItems().addAll(selection);
cboSelectionMode.setOnAction(e ->{
paneForText.getChildren().clear();
if(cboSelectionMode.getValue()=="SINGLE") {
lvItems.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
}
else if(cboSelectionMode.getValue()=="MULTIPLE") {
lvItems.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
}
});
lvItems.getSelectionModel().selectedItemProperty().addListener(ov ->{
paneForText.getChildren().clear();
paneForText.getChildren().add(new Label("Selected items are "));
for(Integer i: lvItems.getSelectionModel().getSelectedIndices()){
Label lb = new Label(items[i]+" ");
paneForText.getChildren().add(lb);
}
});
但我发现了两个问题:
1.当我将选择模型从“SINGLE”更改为“MULTIPLE”时,我的窗格上有一个灰色区域。 我不能删除它,除非我点击它。但如果我点击它,它将被选中。如果我不删除它但只选择其他项目,它也将被选中。除了点击它之外我怎么能删除它?
2.在“MULTIPLE”模型中,它有时会出现错误“ArrayIndexOutOfBoundsException”。我不知道为什么以及如何解决它。