我有这堂课:
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(){
}
我希望在JavaScript(桥接)回调executeScript
中执行命令<a onclick="java.link()">teste</a>
,但我不知道如何在类Bridge中传递webview(WebView.getEngine
):
public void link(){
executeScript(engine, "document.getElementById('main').innerHTML = 'test';"); // engine is WebView
}
有人知道是否有可能或怎么做?
抱歉我的英语非常糟糕= /,我是巴西人! 感谢
答案 0 :(得分:1)
你可以通过构造函数传递它,它是一个可以共享的普通Java对象,即
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