Combobox 限制组合框内的文本输入

Combobox 限制组合框内的文本输入,combobox,javafx,Combobox,Javafx,我需要用正数限制组合框内的文本输入。我搜索了stackoverflow,发现了类似的问题: 唯一的区别是,上面提到的问题只针对裸文本字段。javafx设计者批准的答案是扩展TextField类并覆盖两个方法:replaceText和replaceSelection。如果combobox:TextField实例存储在内部,并且可以作为名为editor的只读属性使用,则此攻击不适用于combobox:TextField实例 那么,在javafx combobox中限制文本输入的推荐方法是什么呢?您可

我需要用正数限制组合框内的文本输入。我搜索了stackoverflow,发现了类似的问题:

唯一的区别是,上面提到的问题只针对裸文本字段。javafx设计者批准的答案是扩展
TextField
类并覆盖两个方法:
replaceText
replaceSelection
。如果combobox:
TextField
实例存储在内部,并且可以作为名为
editor
的只读属性使用,则此攻击不适用于combobox:
TextField
实例


那么,在javafx combobox中限制文本输入的推荐方法是什么呢?

您可以在编辑器属性上注册一个检查方法,以检查是否有任何输入是可接受的

这里我允许编辑,但如果值不在项目列表中,则绘制一个红框

ObservableList<String> myChoices = FXCollections.observableArrayList();

void testComboBoxCheck(VBox box) {
    myChoices.add("A");
    myChoices.add("B");
    myChoices.add("C");

    ComboBox<String> first = new ComboBox<String>();
    first.setItems(myChoices);
    first.setEditable(true);
    first.editorProperty().getValue().textProperty().addListener((v, o, n) -> {
        if (myChoices.contains(n.toUpperCase())) {
            first.setBackground(new Background(new BackgroundFill(Color.rgb(30,30,30), new CornerRadii(0), new Insets(0))));
        } else {
            first.setBackground(new Background(new BackgroundFill(Color.RED, new CornerRadii(0), new Insets(0))));
        }
    });

    box.getChildren().addAll(first);
}
observeListMyChoices=FXCollections.observearraylist();
无效TestComboxCheck(VBox框){
我的选择。添加(“A”);
我的选择。添加(“B”);
我的选择。添加(“C”);
ComboBox first=新建ComboBox();
首先,设置项目(myChoices);
首先,setEditable(true);
first.editorProperty().getValue().textProperty().addListener((v,o,n)->{
if(myChoices.contains(n.toUpperCase())){
首先,背景(新背景(新背景填充(颜色.rgb(30,30,30),新拐角半径(0),新插图(0)));
}否则{
首先,背景(新背景(新背景填充(颜色:红色,新圆角半径(0),新插图(0)));
}
});
box.getChildren().addAll(第一个);
}

如何将文本编辑器与组合框分离并链接其值

    HBox combo = new HBox();

    TextField editor = new TextField();

    ComboBox<String> first = new ComboBox<String>();
    first.setItems(myChoices);
    first.setButtonCell(new ComboBoxListCell<String>(){
        @Override public void updateItem(String s, boolean empty) {
            super.updateItem(s, empty);
            setText(null);
        }
    });

    editor.textProperty().bindBidirectional(first.valueProperty());

    combo.getChildren().addAll(editor, first);
    box.getChildren().addAll(combo);
HBox组合=新的HBox();
TextField编辑器=新建TextField();
ComboBox first=新建ComboBox();
首先,设置项目(myChoices);
first.setButtonCell(新的ComboBoxListCell(){
@重写公共void updateItem(字符串s,布尔值为空){
super.updateItem(s,空);
setText(空);
}
});
editor.textProperty().bindproductive(first.valueProperty());
combo.getChildren().addAll(编辑器,第一个);
box.getChildren().addAll(组合);

现在您已经完全覆盖了文本字段,允许覆盖任何方法等。

因为这个问题从未得到正确的答案,我正在添加一个我已经实现的解决方案,该解决方案限制用户输入匹配正则表达式且短于特定长度的内容(这是可选的)。这是通过向编辑器
TextField
添加一个
ChangeListener
来实现的。任何不匹配的输入都不会写入编辑器
TextField

此示例将用户限制为最多两个数字字符

ComboBox<Integer> integerComboBox = new ComboBox<Integer>();
integerComboBox.setEditable(true);
integerComboBox.getEditor().textProperty()
            .addListener(new ChangeListener<String>() {

                // The max length of the input
                int maxLength = 2;

                // The regular expression controlling the input, in this case we only allow number 0 to 9.
                String restriction = "[0-9]";

                private boolean ignore;

                @Override
                public void changed(
                        ObservableValue<? extends String> observableValue,
                        String oldValue, String newValue) {
                    if (ignore || newValue == null) {
                        return;
                    }

                    if (newValue.length() > maxLength) {
                        ignore = true;
                        integerComboBox.getEditor().setText(
                                newValue.substring(0, maxLength));
                        ignore = false;
                    }

                    if (!newValue.matches(restriction + "*")) {
                        ignore = true;
                        integerComboBox.getEditor().setText(oldValue);
                        ignore = false;
                    }
                }
            });
ComboBox integerComboBox=new ComboBox();
integerComboBox.setEditable(true);
integerComboBox.getEditor().textProperty()
.addListener(新的ChangeListener(){
//输入的最大长度
int maxLength=2;
//控制输入的正则表达式,在本例中,我们只允许数字0到9。
字符串限制=“[0-9]”;
私有布尔忽略;
@凌驾
更改公众假期(

ObserviveValue在与TextField类相关的类似问题中,许多评论员不鼓励这样做。我不知道为什么。在Java8u40中,您也可以使用TextFormatter而不是change listener。