View difference between Paste ID: kAjweLi5 and
SHOW:
|
|
- or go back to the newest paste.
1 | - | |
1 | + | import java.awt.Dimension; |
2 | import java.awt.Point; | |
3 | ||
4 | import javafx.application.Platform; | |
5 | import javafx.beans.value.ChangeListener; | |
6 | import javafx.beans.value.ObservableValue; | |
7 | import javafx.embed.swing.JFXPanel; | |
8 | import javafx.scene.Group; | |
9 | import javafx.scene.Scene; | |
10 | import javafx.scene.web.WebEngine; | |
11 | import javafx.scene.web.WebView; | |
12 | ||
13 | import javax.swing.JButton; | |
14 | import javax.swing.JFrame; | |
15 | import javax.swing.SwingUtilities; | |
16 | ||
17 | public class Test implements ChangeListener<String> { | |
18 | ||
19 | /* Start application */ | |
20 | public static void main(final String[] args) { | |
21 | new Test(); | |
22 | } | |
23 | ||
24 | public Test() { | |
25 | ||
26 | SwingUtilities.invokeLater(new Runnable() { | |
27 | @Override | |
28 | public void run() { | |
29 | initAndShowGUI(); | |
30 | } | |
31 | }); | |
32 | ||
33 | } | |
34 | ||
35 | /* Create a JFrame with a JFXPanel containing the WebView. */ | |
36 | private JFrame frame; | |
37 | ||
38 | private void initAndShowGUI() { | |
39 | // This method is invoked on Swing thread | |
40 | frame = new JFrame("FX"); | |
41 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
42 | ||
43 | frame.getContentPane().setLayout(null); // do the layout manually | |
44 | ||
45 | final JFXPanel fxPanel = new JFXPanel(); | |
46 | ||
47 | frame.add(fxPanel); | |
48 | frame.setVisible(true); | |
49 | ||
50 | fxPanel.setSize(new Dimension(300, 300)); | |
51 | ||
52 | frame.getContentPane().setPreferredSize(new Dimension(300, 300)); | |
53 | frame.pack(); | |
54 | ||
55 | Platform.runLater(new Runnable() { // this will run initFX as | |
56 | // JavaFX-Thread | |
57 | @Override | |
58 | public void run() { | |
59 | initFX(fxPanel); | |
60 | } | |
61 | }); | |
62 | ||
63 | } | |
64 | ||
65 | private WebEngine webEngine; | |
66 | ||
67 | /* Creates a WebView and fires up google.com */ | |
68 | private void initFX(final JFXPanel fxPanel) { | |
69 | Group group = new Group(); | |
70 | Scene scene = new Scene(group); | |
71 | fxPanel.setScene(scene); | |
72 | ||
73 | WebView webView = new WebView(); | |
74 | ||
75 | group.getChildren().add(webView); | |
76 | webView.setMinSize(300, 300); | |
77 | webView.setMaxSize(300, 300); | |
78 | ||
79 | // Obtain the webEngine to navigate | |
80 | webEngine = webView.getEngine(); | |
81 | webEngine.load("http://www.google.com/"); | |
82 | ||
83 | // Add a new listener to the URL property | |
84 | webEngine.locationProperty().addListener(this); | |
85 | ||
86 | } | |
87 | ||
88 | public void exit() { | |
89 | ||
90 | final Test self = this; | |
91 | ||
92 | Platform.runLater(new Runnable() { | |
93 | @Override | |
94 | public void run() { | |
95 | Platform.exit(); | |
96 | } | |
97 | }); | |
98 | ||
99 | SwingUtilities.invokeLater(new Runnable() { | |
100 | ||
101 | @Override | |
102 | public void run() { | |
103 | self.frame.dispose(); | |
104 | } | |
105 | ||
106 | }); | |
107 | } | |
108 | ||
109 | ||
110 | /** | |
111 | * Fires when URL changes | |
112 | */ | |
113 | @Override | |
114 | public void changed(ObservableValue<? extends String> arg0, String oldUrl, String newUrl) { | |
115 | ||
116 | // Conditions should be here (if url.equals(successUrl) .. | |
117 | this.exit(); | |
118 | ||
119 | } | |
120 | ||
121 | } |