我想知道是否有一个组合框选项来防止选择组合框未包含的项目,或者如果我必须自己过滤。
正如我们在下面的例子中看到的,所选项目的索引是“-1” 在这样做之前有没有办法预防这种情况?
我的意思是,我知道如何用代码来做到这一点,我想知道是否可以激活组合框选项来进行过滤
编辑1:注册项目为“1”,“2”,“3”,“4”
“xyz”(对我来说)的预期结果应该是:根本没有选择,因为“xyz”不是组合框项目
package fr.meteo.pacome.adm;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Maintest extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group(), 450, 250);
ObservableList<String> list = FXCollections.observableArrayList("1","2","3","4");
ComboBox<String> emailComboBox = new ComboBox<String>(list);
emailComboBox.getSelectionModel().select("notinlistChoice");
System.out.println(emailComboBox.getSelectionModel().getSelectedIndex());
GridPane grid = new GridPane();
grid.setVgap(4);
grid.setHgap(10);
grid.setPadding(new Insets(5, 5, 5, 5));
grid.add(new Label("To: "), 0, 0);
grid.add(emailComboBox, 1, 0);
Group root = (Group) scene.getRoot();
root.getChildren().add(grid);
stage.setScene(scene);
stage.show();
}
}
从您提供的演示中,我相信如果该值与组合框中的任何选项都不匹配,您不希望该值显示在按钮单元格中。
实现此目的的一种方法是监听 selectedItem 属性,并在提供的值不匹配时重置选择。
emailComboBox.getSelectionModel().selectedItemProperty().addListener((obs, old, val) -> {
if (val != null && !emailComboBox.getItems().stream().anyMatch(val::equals)) {
emailComboBox.getSelectionModel().select(null);
}
});
完整的工作演示如下:
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Maintest extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
ObservableList<String> list = FXCollections.observableArrayList("1", "2", "3", "4");
ComboBox<String> emailComboBox = new ComboBox<String>(list);
emailComboBox.getSelectionModel().selectedItemProperty().addListener((obs, old, val) -> {
if (val != null && !emailComboBox.getItems().stream().anyMatch(val::equals)) {
emailComboBox.getSelectionModel().select(null);
}
});
emailComboBox.getSelectionModel().select("notinlistChoice");
GridPane grid = new GridPane();
grid.setVgap(4);
grid.setHgap(10);
grid.setPadding(new Insets(5, 5, 5, 5));
grid.add(new Label("To: "), 0, 0);
grid.add(emailComboBox, 1, 0);
TextField textField = new TextField();
Button button = new Button("Apply");
button.setOnAction(e -> emailComboBox.getSelectionModel().select(textField.getText()));
HBox hb = new HBox(10, new Label("Type a value to set: "), textField, button);
VBox root = new VBox(10, hb, grid);
root.setPadding(new Insets(20));
Scene scene = new Scene(root, 450, 250);
stage.setScene(scene);
stage.show();
}
}