2015-06-19 53 views
3

我正在使用JavaFX和場景生成器,並且我有一個帶有文本框的窗體。其中三個文本字段從字符串解析到雙打。JavaFX輸入驗證文本域

我希望他們成爲學校標記,因此他們應該只允許在1.0和6.0之間。用戶不應該被允許寫「2.34.4」之類的東西,但是像「5.5」或「2.9」這樣的東西可以。

驗證了分析的領域:

public void validate(KeyEvent event) { 
    String content = event.getCharacter(); 
    if ("123456.".contains(content)) { 
      // No numbers smaller than 1.0 or bigger than 6.0 - How? 
    } else { 
     event.consume(); 
    } 
} 

如何測試,如果用戶輸入正確的值?

我已經在Stackoverflow和Google上搜索過,但是我沒有找到令人滿意的解決方案。

回答

7
textField.focusedProperty().addListener((arg0, oldValue, newValue) -> { 
     if (!newValue) { //when focus lost 
      if(!textField.getText().matches("[1-5]\\.[0-9]|6\\.0")){ 
       //when it not matches the pattern (1.0 - 6.0) 
       //set the textField empty 
       textField.setText(""); 
      } 
     } 

    }); 

你還可以將模式改變爲[1-5](\.[0-9]){0,1}|6(.0){0,1}然後1,2,3,4,5,6也將是確定的(不僅1.0,2.0,...

更新 這裏是值1(.00)到6一個小的測試應用程序(.00):

public class JavaFxSample extends Application { 

@Override 
public void start(Stage primaryStage) { 
    primaryStage.setTitle("Enter number and hit the button"); 
    GridPane grid = new GridPane(); 
    grid.setAlignment(Pos.CENTER); 
    Label label1To6 = new Label("1.0-6.0:"); 
    grid.add(label1To6, 0, 1); 
    TextField textField1To6 = new TextField(); 

    textField1To6.focusedProperty().addListener((arg0, oldValue, newValue) -> { 
     if (!newValue) { // when focus lost 
       if (!textField1To6.getText().matches("[1-5](\\.[0-9]{1,2}){0,1}|6(\\.0{1,2}){0,1}")) { 
        // when it not matches the pattern (1.0 - 6.0) 
        // set the textField empty 
        textField1To6.setText(""); 
       } 
      } 
     }); 
    grid.add(textField1To6, 1, 1); 
    grid.add(new Button("Hit me!"), 2, 1); 
    Scene scene = new Scene(grid, 300, 275); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 

public static void main(String[] args) { 
    launch(args); 
} 

} 
+0

非常感謝@griFlo,但仍然有一個問題。我不能輸入像「5.0」,「4.7」,「2.8」或「1.9」這樣的值。你知道我該如何告訴程序應該只有兩位小數? – BRsmover

+0

@BRsmover你說的所有的值應該被允許(實際上,我只是用上面的代碼測試它,它的工作原理就是找到)。你的意思是兩位小數?你可以使用這個正則表達式[[1-5](\。[0-9] {1,2}){0,1} | 6(.0 {1,2}){0,1}'(如果我正確理解你的話) – griFlo

+0

對不起,我一定誤解了一些東西......一切都很好!非常感謝你! – BRsmover

4

我不會建議你使用KeyEvent。

您應該使用更經典的方式,例如在用戶填寫文本字段或單擊保存按鈕時驗證用戶輸入。

/** 
* Called this when the user clicks on the save button or finish to fill the text field. 
*/ 
private void handleSave() { 
     // If the inputs are valid we save the data 
     if(isInputValid()){ 
      note=(DOUBLE.parseDouble(textField.getText())); 
     }else // do something such as notify the user and empty the field 
} 

/** 
* Validates the user input in the text fields. 
* 
* @return true if the input is valid 
*/ 
private boolean isInputValid() { 
    Boolean b= false; 
    if (!(textField.getText() == null || textFiled.getText().length() == 0)) { 
     try { 
      // Do all the validation you need here such as 
      Double d = Double.parseInt(textFiled.getText()); 
      if (1.0<d<6.0){ 
       b=true; 
      } 
     } catch (NumberFormatException e) { 
     } 
    return b; 
} 
+0

我會詳細說明這一點。我會調用驗證方法3次:保存操作,文本字段的操作事件以及失去焦點。通過這種方式,用戶可以在發生錯誤的情況下立即做出響應,此外還可以防止保存不良數據。 –