1

I have this class:

public Palco()  {
    super();
    initComponents();
    setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);

}


private void initComponents() {
    createScene();

    progressBar.setPreferredSize(new Dimension(150, 18));
    progressBar.setStringPainted(true);

    JPanel topBar = new JPanel(new BorderLayout(5, 0));
    topBar.setBorder(BorderFactory.createEmptyBorder(3, 5, 3, 5));
    topBar.add(txtURL, BorderLayout.CENTER);
    topBar.add(btnGo, BorderLayout.EAST);


    statusBar.setBorder(BorderFactory.createEmptyBorder(3, 5, 3, 5));
    statusBar.add(lblStatus, BorderLayout.CENTER);
    statusBar.add(progressBar, BorderLayout.EAST);

    //panel.add(topBar, BorderLayout.NORTH);
    panel.add(jfxPanel, BorderLayout.CENTER);
    panel.add(statusBar, BorderLayout.SOUTH);

    getContentPane().add(panel);

    setTitle("My App");

    setPreferredSize(new Dimension(1024, 600));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();

}

private void createScene() {

    Platform.runLater(new Runnable() {

        public void run() {

            WebView view = new WebView();
            engine = view.getEngine();
            engine.setJavaScriptEnabled(true);


            engine.titleProperty().addListener(new ChangeListener<String>() {

                public void changed(ObservableValue<? extends String> observable, String oldValue, final String newValue) {
                    SwingUtilities.invokeLater(new Runnable() {
                         String title = null;
                        public void run() {
                            if(newValue == null){
                              title = "Carregando...";
                            } else {
                              title = newValue;
                            }
                            Palco.this.setTitle(title);
                        }
                    });
                }
            });

            engine.setOnStatusChanged(new EventHandler<WebEvent<String>>() {

                public void handle(final WebEvent<String> event) {
                    SwingUtilities.invokeLater(new Runnable() {

                        public void run() {
                           // lblStatus.setText(event.getData());
                        }
                    });
                }
            });

            engine.locationProperty().addListener(new ChangeListener<String>() {

                public void changed(ObservableValue<? extends String> ov, String oldValue, final String newValue) {
                    SwingUtilities.invokeLater(new Runnable() {

                        public void run() {
                            txtURL.setText(newValue);
                        }
                    });
                }
            });

            engine.getLoadWorker().workDoneProperty().addListener(new ChangeListener<Number>() {

                public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, final Number newValue) {

                    SwingUtilities.invokeLater(new Runnable() {

                        public void run() {
                             if(newValue.intValue() < 100){
                                 statusBar.setVisible(true);
                             } else {
                                 statusBar.setVisible(false);

                             }
                            progressBar.setValue(newValue.intValue());

                        }
                    });
                }
            });


            engine.documentProperty().addListener(new ChangeListener<Document>() {
                 public void changed(ObservableValue<? extends Document> prop, Document oldDoc, Document newDoc) {
                     MainTiles tiles = new MainTiles(engine);
                    tiles.ShowCategory(0);


                }
              });

            engine.getLoadWorker()
                    .exceptionProperty()
                    .addListener(new ChangeListener<Throwable>() {

                        public void changed(ObservableValue<? extends Throwable> o, Throwable old, final Throwable value) {
                            if (engine.getLoadWorker().getState() == FAILED) {
                                SwingUtilities.invokeLater(new Runnable() {
                                     public void run() {
                                        JOptionPane.showMessageDialog(
                                                panel,
                                                (value != null) ?
                                                engine.getLocation() + "\n" + value.getMessage() :
                                                engine.getLocation() + "\nUnexpected error.",
                                                "Loading error...",
                                                JOptionPane.ERROR_MESSAGE);
                                    }
                                });
                            }
                        }
                    });

           engine.getLoadWorker().stateProperty().addListener(
                   new ChangeListener<State>() {
                       @Override
                       public void changed(ObservableValue<? extends State> ov,
                           State oldState, State newState) {
                           JSObject jsobj = (JSObject) engine.executeScript("window");
                           jsobj.setMember("java", new Bridge());


                       }

                       }
               );

            jfxPanel.setScene(new Scene(view));
        }
    });
}

public void loadURL(final String url) {
    Platform.runLater(new Runnable() {
        public void run() {
            String tmp = toURL(url);

            if (tmp == null) {
                tmp = toURL("http://" + url);
            }

            engine.load(tmp);
        }
    });
}

private static String toURL(String str) {
    try {
        return new URL(str).toExternalForm();
    } catch (MalformedURLException exception) {
            return null;
    }
}


public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            Palco browser = new Palco();
            browser.setVisible(true);
            String url = getClass().getResource("html/index.html").toExternalForm();
            browser.loadURL(url);
       }     
   });
}


public static void changeScene(){

}

And I wanted execute the command executeScript in JavaScript (bridge) callback <a onclick="java.link()">teste</a>, but I dont know how pass the webview (WebView.getEngine) in class Bridge:

public void link(){
    executeScript(engine, "document.getElementById('main').innerHTML = 'test';"); // engine is WebView

}

Does anyone know if it is possible or how to do?

Sorry my english is very bad =/, I'm Brazilian! Thanks

1 Answer 1

1

You could it pass it via a constructor, it's a normal Java object which can be shared, i.e.

public class Bridge{
    WebEngine engine;    // add a new field

    public Bridge(WebEngine engine){
        this.engine = engine;
    }
}

// ...

JSObject jsobj = (JSObject) engine.executeScript("window");
jsobj.setMember("java", new Bridge(engine));   // pass it here
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.