2017-09-25 227 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