1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
public class IHMTest extends JPanel {
public IHMTest() {
super(new BorderLayout());
JFXPanel jfxPanel = new JFXPanel();
JWebView webView = new JWebView(jfxPanel);
JPanel addressPanel = new JPanel(new GridBagLayout());
JTextField field=new JTextField("http://www.google.fr");
addressPanel.add(field, new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2,2,2,1), 0, 0));
field.addActionListener(e-> webView.setURL(field.getText()));
JButton btnLaunch = new JButton("Lancer");
btnLaunch.addActionListener(e-> webView.setURL(field.getText()));
webView.setURLChangeListener(url-> field.setText(url));
webView.setURL(field.getText());
addressPanel.add(btnLaunch, new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1,2,2,2), 0, 0));
add(addressPanel, BorderLayout.NORTH);
add(jfxPanel, BorderLayout.CENTER);
}
public void actionbtnQuitEcran() {
Platform.exit();
}
private static class JWebView {
private WebView webView;
public JWebView(JFXPanel jfxPanel) {
Platform.runLater(() -> {
webView = new WebView();
Scene scene = new Scene(webView);
jfxPanel.setScene(scene);
});
}
public void setURLChangeListener(Consumer<String> callback) {
Platform.runLater(() -> webView.getEngine().locationProperty().addListener((observable, oldValue, newValue) -> {
SwingUtilities.invokeLater(()->callback.accept(newValue));
}));
}
public void setURL(String url) {
Platform.runLater(() -> webView.getEngine().load(url));
}
}
public static void main(String[] args) {
JFrame frame=new JFrame("WebView Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new IHMTest());
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} |
Partager