Skip to content

Overriding the default EventHandler #80

@JordanMartinez

Description

@JordanMartinez

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions