2014-11-14 28 views
0

我需要限制正數組合框內的文本輸入。我搜索了這個stackoverflow,發現類似的問題:Recommended way to restrict input in JavaFX textfield限制組合框內的文本輸入

唯一的區別是,提到的問題地址裸露的文本字段。 javafx設計人員批准的答案是擴展TextField類並覆蓋幾個方法:replaceTextreplaceSelection。這個黑客無法使用組合框:TextField如果存儲在內部並且可用,則爲只讀屬性名稱爲editor

那麼在javafx組合框內限制文本輸入的推薦方法是什麼?

回答

0

您可能會在editor屬性中註冊一個檢查方法,以檢查是否有任何輸入符合要求。

這裏我允許編輯,但如果值不在項目列表中,則繪製一個紅框。

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); 
} 

enter image description here enter image description here

+0

許多與TextField類相關的問題的評論者都不願意這樣做。我不知道爲什麼。 – ayvango

0

如何從ComboBox decouplign文本編輯器和鏈接他們的價值觀?

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); 

現在你必須在文本字段,允許覆蓋任何方法等

0

由於這個問題,問題一直沒有得到正確的答案我添加一個解決方案,我實現了限制用戶全連軋與正則表達式匹配並且比特定長度短的輸入(這是可選的)。這是通過將ChangeListener添加到編輯器TextField來完成的。任何不匹配的輸入都不會被寫入編輯器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; 
        } 
       } 
      }); 
相關問題