Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting HTML textarea onkeyup event in JavaFX WebView

I want to print the content of an HTML textarea inside WebView as soon as I type it.
PS: I tried listening to the keyEvent of webView for some reason it didn't work.

like image 744
Vinod CG Avatar asked May 18 '12 04:05

Vinod CG


1 Answers

  1. If you are trying to print content of JavaFX TextArea object into WebView then you should add listeners to TextArea not WebView.

  2. If you are trying to listen for event in HTML TextArea tag inside HTML page in WebView you should add listeners to document model:

    // we need this to wait till document load
    webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
        public void changed(ObservableValue ov, State oldState, State newState) {
            if (newState == Worker.State.SUCCEEDED) {
    
                // note next classes are from org.w3c.dom domain
                EventListener listener = new EventListener() {
                    public void handleEvent(Event ev) {
                        System.out.println(ev.getType());
                    }
                };
    
                Document doc = webEngine.getDocument();
                Element el = doc.getElementById("textarea");
                ((EventTarget) el).addEventListener("keypress", listener, false);
            }
        }
    });
    webEngine.loadContent("<textarea id='textarea'></textarea>");
    
like image 171
Sergey Grinev Avatar answered Sep 28 '22 03:09

Sergey Grinev