Well I'm studying Java FX, but this time I'm using FXML in NetBeans, then I want to restric the Keys allowed by a TextField. Like just numbers or just Letters.
I found This , then I created a new class, then put that code (the checked as correct in the link), i extend TextField, but when I run the code, throws a exception, I think is because SceneBuilder doesn't have my Class.
Update i found a similar code for 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);
}
}
That code above works fine in a Java FX app, but I need one to use in Java FXML, one guy below Post a similar code, it compiles, no trows exception, but doesn't work, or i don't know how to implement it.
If you want to limit your text to just ALPHA/NUMERIC you can use a textformatter like this:
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;
};
}
}
That creates a formatter than only allows numbers and letters - you can adjust the pattern to your needs.
You attach it to your textfield like this....
@FXML
private TextField myTextField;
@FXML
private void initialize() {
//Create a alpha field which max length of 4
myTextField.setTextFormatter(new AlphaNumericTextFormatter(4));
}