2 Replies Latest reply: May 7, 2012 3:52 PM by 935401 RSS

    Reading HTML form values from WebView

    935401
      Hey Guys!

      I need to read out some user inputs in a HTML form within a WebView JavaFX control. My idea was to access the DOM model and read out the value attribute (like: WebView.getEngine().getDocument().getElementById(foo).getAttribute(bla)).

      But all I get is the values that were set from the HTML code (<input type='text' value='foo'>) or by the setAttribute method. Inputs that the user made during runtime appear not to be saved in the DOM.

      Do I need to call some function to rebuild the DOM Tree before reading out my values, or what is wrong?

      Thanks!

      PS: I'm running JavaFX 2.1 on OS X.
        • 1. Re: Reading HTML form values from WebView
          jsmith
          When are you invoking WebView.getEngine().getDocument().getElementById(foo).getAttribute(bla))?
          Can you publish a short executable example?

          I think you will need to have a JavaScript function which monitors the fields you are interested in and sends the data back to your Java app.
          For examples see => https://gist.github.com/1422815 "Sample DatePicker for JavaFX using jQuery UI"
          Or => http://docs.oracle.com/javafx/2/webview/jfxpub-webview.htm "Making Upcalls from JavaScript to JavaFX"
          • 2. Re: Reading HTML form values from WebView
            935401
            Code incoming ;)
            public class SheetViewer extends Stage {
                
                WebEngine webengine;
                
                SheetViewer() {
                    WebView webview = new WebView();
                    webengine = webview.getEngine();
                    GridPane gridpane = new GridPane();
                    
                    webengine.loadContent("<html><body><input type='text' name='textinput' value='test'></body></html>");
                    
                    gridpane.add(webview, 1, 1);
                    
                    Button button = new Button("okay");
                    
                    button.setOnAction(new EventHandler<ActionEvent>() {
            
                        @Override
                        public void handle(ActionEvent t) {
                            webengine.getDocument().normalize();
                            NodeList inputs = webengine.getDocument().getElementsByTagName("input");
                            Element input;
                            for(int i = 0; i < inputs.getLength(); i++) {
                                input = (Element) inputs.item(i);
                                System.out.print(input.getAttribute("name") + ": ");
                                System.out.println(input.getAttribute("value"));
                            }
                        }
                    });
                    
                    gridpane.add(button, 2, 1);
                    
                    this.setScene(new Scene(gridpane));
                    this.show();
                }
            }
            That's not my original code (for its too long), but it shows the same problem:

            input.getAttribute("value") will always return "test", no matter what the user has entered (or not entered) during runtime.

            I have used .net before (which shows 'surpisingly' some similarities to JavaFX ;) ), in which you could read out user inputs from a webbrowser object like that, so I'd be happy to find a javascript-free solution here (if that is possible).

            Edited by: 932398 on 07.05.2012 13:51