Hi Tomas,
I'm trying to create a StyleClassedTextArea that only inserts a specific list of characters (using a regex match) into its text once that text has been initialized. The following is the code I thought would achieve this:
StyleClassedTextArea scta = new StyleClassedTextArea();
scta.replaceText(0,0, "Initialized Text");
scta.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
String event_text = event.getText();
String regex_pattern = "[a-z]";
if (event_text.matches(regex_pattern)) {
scta.insertText(scta.getCaretPosition(), event_text);
}
event.consume();
}
});
This is what I expect:
- When I type "a" it inserts "a"
- When I type "A" it doesn't insert anything
I ran the code:
- When I type "a" it inserts "aa"
- When I type "A" it inserts "A"
What am I doing wrong? I thought my code would override the default OnKeyPressed EventHandler.
Hi Tomas,
I'm trying to create a StyleClassedTextArea that only inserts a specific list of characters (using a regex match) into its text once that text has been initialized. The following is the code I thought would achieve this:
This is what I expect:
I ran the code:
What am I doing wrong? I thought my code would override the default OnKeyPressed EventHandler.