2017-09-25 90 views
0

嗯,我正在學習Java FX,但是這次我在NetBeans中使用FXML,然後我想限制TextField允許的鍵。就像數字或只是字母。過濾器鍵盤TextField Java FXML

我發現This,然後我創建了一個新的類,然後把這個代碼(檢查爲正確的鏈接),我擴展TextField,但是當我運行代碼時,拋出一個異常,我認爲是因爲SceneBuilder doesn沒有我的班級。

更新,我發現對Java FX類似的代碼:

import java.util.function.UnaryOperator; 
import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.TextField; 
import javafx.scene.control.TextFormatter; 
import javafx.scene.control.TextFormatter.Change; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 

/** 
* 
* @author Alejandro 
*/ 
public class JavaFXApplication2 extends Application { 

@Override 
public void start(Stage primaryStage) { 

    TextField textField = new TextField(); 
    TextFormatter<String> textFormatter = getTextFormatter(); 
    textField.setTextFormatter(textFormatter); 

    VBox root = new VBox(); 

    root.getChildren().add(textField); 

    Scene scene = new Scene(root, 300, 250); 

    primaryStage.setTitle("TextFormat"); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 

private TextFormatter<String> getTextFormatter() { 
    UnaryOperator<Change> filter = getFilter(); 
    TextFormatter<String> textFormatter = new TextFormatter<>(filter); 
    return textFormatter; 
} 

private UnaryOperator<Change> getFilter() { 
    return change -> { 
     String text = change.getText(); 

     if (!change.isContentChange()) { 
      return change; 
     } 

     if (text.matches("[a-z]*") || text.isEmpty()) { 
      return change; 
     } 

     return null; 
    }; 
} 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 
    launch(args); 
} 

} 

上面的代碼工作正常,在Java FX應用程序,但我需要一個在Java中FXML使用,後下一個類似的代碼一個人,它編譯,沒有trows異常,但不起作用,或者我不知道如何實現它。

+1

這種聯繫是很老。你應該爲這種功能使用一個'TextFormatter'。參見例如https://stackoverflow.com/questions/40472668/numeric-textfield-for-integers-in-javafx-8-with-textformatter-and-or-unaryoperat –

回答

0

如果你想你的文字限制,只是字母/數字,你可以使用的TextFormatter這樣的:

public class MaxLengthTextFormatter extends TextFormatter<String> { 
    private int maxLength; 

public MaxLengthTextFormatter(int maxLength) { 
    super(new UnaryOperator<TextFormatter.Change>() { 
     @Override 
     public TextFormatter.Change apply(TextFormatter.Change change) { 
      //If the user is deleting the text (i.e. full selection, ignore max to allow the 
      //change to happen) 
      if(change.isDeleted()) { 
       //if the user is pasting in, then the change could be longer 
       //ensure it stops at max length of the field 
       if(change.getControlNewText().length() > maxLength){ 
        change.setText(change.getText().substring(0, maxLength)); 
       } 

      }else if (change.getControlText().length() + change.getText().length() >= maxLength) { 
       int maxPos = maxLength - change.getControlText().length(); 
       change.setText(change.getText().substring(0, maxPos)); 
      } 
      return change; 
     } 
    }); 
    this.maxLength = maxLength; 
} 

public int getMaxLength() 
{ 
    return maxLength; 
} 

}

public class AlphaNumericTextFormatter extends TextFormatter<String> { 

    /** The Constant ALPHA_NUMERIC_ONLY. */ 
    //private static final String ALPHA_NUMERIC_ONLY = "^[a-zA-Z0-9]*$"; 
    /** MAKE NUMERIC ONLY **/ 
    private static final String DIGITS_ONLY = "^[0-9]*$"; 

    /** 
    * Instantiates a new alpha numeric text formatter. 
    */ 
    public AlphaNumericTextFormatter() { 
     super(applyFilter(null)); 
    } 

    /** 
    * Instantiates a new alpha numeric text formatter. 
    * 
    * @param maxLength 
    *   the max length 
    */ 
    public AlphaNumericTextFormatter(int maxLength) { 
     super(applyFilter(new MaxLengthTextFormatter(maxLength).getFilter())); 
    } 

    /** 
    * Apply filter. 
    * 
    * @param filter 
    *   the filter 
    * @return the unary operator 
    */ 
    private static UnaryOperator<Change> applyFilter(UnaryOperator<Change> filter) { 
     return change -> { 
      if (change.getControlNewText() != null && change.getControlNewText().matches(DIGITS_ONLY)) { 
       if (filter != null) { 
        filter.apply(change); 
       } 
       return change; 
      } 
      return null; 
     }; 
    } 

} 

,創建一個格式化比只允許數字和字母 - 您可以根據需要調整圖案。

你把它連接到你的文本框這樣的....

@FXML 
private TextField myTextField; 


@FXML 
private void initialize() { 
    //Create a alpha field which max length of 4 
    myTextField.setTextFormatter(new AlphaNumericTextFormatter(4)); 
} 
+0

感謝您的回答,代碼的作品,但我沒有足夠的解釋,我想捕捉KeyEvent並且不允許在TextField中顯示。 – Palomino9r

+0

不知道我明白你在問什麼。你想限制所有的鍵輸入?如果是這樣,請將文本字段設置爲不可編輯。你在問什麼? –

+0

並非所有的鍵輸入,只是限制一些鍵,在TextField中使它只是數字或只是A-Z。用於驗證過程,例如當您在登錄時鍵入您的姓名,系統只讓您鍵入字母,而不是數字既不是特殊字符。 – Palomino9r