Libraryless. Click here for Pure Java version (233L/3K/10K).
1 | import javafx.application.Application; |
2 | import javafx.application.Platform; |
3 | import javafx.beans.value.ChangeListener; |
4 | import javafx.beans.value.ObservableValue; |
5 | import javafx.collections.ListChangeListener; |
6 | import javafx.collections.ListChangeListener.Change; |
7 | import javafx.concurrent.Worker.State; |
8 | import javafx.event.ActionEvent; |
9 | import javafx.event.Event; |
10 | import javafx.event.EventHandler; |
11 | import javafx.geometry.HPos; |
12 | import javafx.geometry.Pos; |
13 | import javafx.geometry.VPos; |
14 | import javafx.scene.Node; |
15 | import javafx.scene.Scene; |
16 | import javafx.scene.control.Button; |
17 | import javafx.scene.control.ComboBox; |
18 | import javafx.scene.control.Hyperlink; |
19 | import javafx.scene.image.Image; |
20 | import javafx.scene.image.ImageView; |
21 | import javafx.scene.layout.HBox; |
22 | import javafx.scene.layout.Priority; |
23 | import javafx.scene.layout.Region; |
24 | import javafx.scene.paint.Color; |
25 | import javafx.scene.web.PopupFeatures; |
26 | import javafx.scene.web.WebEngine; |
27 | import javafx.scene.web.WebHistory; |
28 | import javafx.scene.web.WebHistory.Entry; |
29 | import javafx.scene.web.WebView; |
30 | import javafx.stage.Stage; |
31 | import javafx.util.Callback; |
32 | import netscape.javascript.JSObject; |
33 | |
34 | public class main extends Application { |
35 | |
36 | private Scene scene; |
37 | |
38 | @Override |
39 | public void start(Stage stage) { |
40 | // create scene |
41 | stage.setTitle("Web View"); |
42 | scene = new Scene(new Browser(), 750, 500, Color.web("#666970")); |
43 | stage.setScene(scene); |
44 | // apply CSS style |
45 | scene.getStylesheets().add("webviewsample/BrowserToolbar.css"); |
46 | // show stage |
47 | stage.show(); |
48 | } |
49 | |
50 | public static void main(String[] args) { |
51 | launch(main.class, args); |
52 | } |
53 | } |
54 | |
55 | class Browser extends Region { |
56 | |
57 | private HBox toolBar; |
58 | private static String[] imageFiles = new String[]{ |
59 | "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_1000871&contentType=image/jpeg", |
60 | "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_1000871&contentType=image/jpeg", |
61 | "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_1000871&contentType=image/jpeg", |
62 | "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_1000871&contentType=image/jpeg", |
63 | "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_1000871&contentType=image/jpeg", |
64 | }; |
65 | private static String[] captions = new String[]{ |
66 | "Products", |
67 | "Blogs", |
68 | "Documentation", |
69 | "Partners", |
70 | "Help" |
71 | }; |
72 | private static String[] urls = new String[]{ |
73 | "http://www.oracle.com/products/index.html", |
74 | "http://blogs.oracle.com/", |
75 | "http://docs.oracle.com/javase/index.html", |
76 | "http://tinybrain.de/", |
77 | "http://tinybrain.blog.de/" |
78 | }; |
79 | final ImageView selectedImage = new ImageView(); |
80 | final Hyperlink[] hpls = new Hyperlink[captions.length]; |
81 | final Image[] images = new Image[imageFiles.length]; |
82 | final WebView browser = new WebView(); |
83 | final WebEngine webEngine = browser.getEngine(); |
84 | final Button showPrevDoc = new Button("Toggle Previous Docs"); |
85 | final WebView smallView = new WebView(); |
86 | final ComboBox comboBox = new ComboBox(); |
87 | private boolean needDocumentationButton = false; |
88 | |
89 | public Browser() { |
90 | //apply the styles |
91 | getStyleClass().add("browser"); |
92 | |
93 | for (int i = 0; i < captions.length; i++) { |
94 | // create hyperlinks |
95 | Hyperlink hpl = hpls[i] = new Hyperlink(captions[i]); |
96 | Image image = images[i] = |
97 | new Image(imageFiles[i], 100, 0, false, false); |
98 | hpl.setGraphic(new ImageView(image)); |
99 | final String url = urls[i]; |
100 | final boolean addButton = (hpl.getText().equals("Documentation")); |
101 | |
102 | // process event |
103 | hpl.setOnAction(new EventHandler<ActionEvent>() { |
104 | @Override |
105 | public void handle(ActionEvent e) { |
106 | needDocumentationButton = addButton; |
107 | webEngine.load(url); |
108 | } |
109 | }); |
110 | } |
111 | |
112 | comboBox.setPrefWidth(60); |
113 | |
114 | // create the toolbar |
115 | toolBar = new HBox(); |
116 | toolBar.setAlignment(Pos.CENTER); |
117 | toolBar.getStyleClass().add("browser-toolbar"); |
118 | toolBar.getChildren().add(comboBox); |
119 | toolBar.getChildren().addAll(hpls); |
120 | toolBar.getChildren().add(createSpacer()); |
121 | |
122 | //set action for the button |
123 | showPrevDoc.setOnAction(new EventHandler() { |
124 | @Override |
125 | public void handle(Event t) { |
126 | webEngine.executeScript("toggleDisplay('PrevRel')"); |
127 | } |
128 | }); |
129 | |
130 | smallView.setPrefSize(120, 80); |
131 | |
132 | //handle popup windows |
133 | webEngine.setCreatePopupHandler( |
134 | new Callback<PopupFeatures, WebEngine>() { |
135 | @Override public WebEngine call(PopupFeatures config) { |
136 | smallView.setFontScale(0.8); |
137 | if (!toolBar.getChildren().contains(smallView)) { |
138 | toolBar.getChildren().add(smallView); |
139 | } |
140 | return smallView.getEngine(); |
141 | } |
142 | } |
143 | ); |
144 | |
145 | //process history |
146 | final WebHistory history = webEngine.getHistory(); |
147 | history.getEntries().addListener(new |
148 | ListChangeListener<WebHistory.Entry>(){ |
149 | @Override |
150 | public void onChanged(Change<? extends Entry> c) { |
151 | c.next(); |
152 | for (Entry e : c.getRemoved()) { |
153 | comboBox.getItems().remove(e.getUrl()); |
154 | } |
155 | for (Entry e : c.getAddedSubList()) { |
156 | comboBox.getItems().add(e.getUrl()); |
157 | } |
158 | } |
159 | }); |
160 | |
161 | //set the behavior for the history combobox |
162 | comboBox.setOnAction(new EventHandler<ActionEvent>() { |
163 | @Override |
164 | public void handle(ActionEvent ev) { |
165 | int offset = |
166 | comboBox.getSelectionModel().getSelectedIndex() |
167 | - history.getCurrentIndex(); |
168 | history.go(offset); |
169 | } |
170 | }); |
171 | |
172 | |
173 | |
174 | // process page loading |
175 | webEngine.getLoadWorker().stateProperty().addListener( |
176 | new ChangeListener<State>() { |
177 | @Override |
178 | public void changed(ObservableValue<? extends State> ov, |
179 | State oldState, State newState) { |
180 | toolBar.getChildren().remove(showPrevDoc); |
181 | if (newState == State.SUCCEEDED) { |
182 | JSObject win = |
183 | (JSObject) webEngine.executeScript("window"); |
184 | win.setMember("app", new JavaApp()); |
185 | if (needDocumentationButton) { |
186 | toolBar.getChildren().add(showPrevDoc); |
187 | } |
188 | } |
189 | } |
190 | } |
191 | ); |
192 | |
193 | // load the home page |
194 | webEngine.load("http://www.oracle.com/products/index.html"); |
195 | |
196 | //add components |
197 | getChildren().add(toolBar); |
198 | getChildren().add(browser); |
199 | } |
200 | |
201 | // JavaScript interface object |
202 | public class JavaApp { |
203 | |
204 | public void exit() { |
205 | Platform.exit(); |
206 | } |
207 | } |
208 | |
209 | private Node createSpacer() { |
210 | Region spacer = new Region(); |
211 | HBox.setHgrow(spacer, Priority.ALWAYS); |
212 | return spacer; |
213 | } |
214 | |
215 | @Override |
216 | protected void layoutChildren() { |
217 | double w = getWidth(); |
218 | double h = getHeight(); |
219 | double tbHeight = toolBar.prefHeight(w); |
220 | layoutInArea(browser,0,0,w,h-tbHeight,0,HPos.CENTER,VPos.CENTER); |
221 | layoutInArea(toolBar,0,h-tbHeight,w,tbHeight,0,HPos.CENTER,VPos.CENTER); |
222 | } |
223 | |
224 | @Override |
225 | protected double computePrefWidth(double height) { |
226 | return 750; |
227 | } |
228 | |
229 | @Override |
230 | protected double computePrefHeight(double width) { |
231 | return 600; |
232 | } |
233 | } |
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1000870 |
Snippet name: | JavaFX WebView test |
Eternal ID of this version: | #1000870/1 |
Text MD5: | 20e73bc86d29967966b3cec812b1e51a |
Transpilation MD5: | 20e73bc86d29967966b3cec812b1e51a |
Author: | stefan |
Category: | javax |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-09-03 17:48:39 |
Source code size: | 8342 bytes / 233 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 999 / 1005 |
Referenced in: | [show references] |