Created
January 6, 2013 01:53
-
-
Save anonymous/4464747 to your computer and use it in GitHub Desktop.
JavaFX using WebView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import javafx.application.Application; | |
import javafx.beans.value.ChangeListener; | |
import javafx.beans.value.ObservableValue; | |
import javafx.concurrent.Worker; | |
import javafx.concurrent.Worker.State; | |
import javafx.event.ActionEvent; | |
import javafx.event.EventHandler; | |
import javafx.geometry.Insets; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.layout.HBox; | |
import javafx.scene.layout.VBox; | |
import javafx.scene.web.WebEngine; | |
import javafx.scene.web.WebView; | |
import javafx.stage.Stage; | |
import javafx.util.Callback; | |
/** | |
* | |
* @author tomo | |
*/ | |
public class HelloSampleTest extends Application{ | |
private WebEngine engine; | |
private boolean isConfirmOK; | |
@Override | |
public void start(Stage stage) throws Exception { | |
WebView webView = new WebView(); | |
engine = webView.getEngine(); | |
engine.load("http://localhost:8080/HelloSample"); | |
VBox box = new VBox(); | |
box.getChildren().add(webView); | |
Button btnLoad = new Button("load Hello Screen"); | |
final Button btnHello = new Button("Greet Hello"); | |
btnHello.setDisable(true); | |
final Button btnMorning = new Button("Greet おはよう"); | |
btnMorning.setDisable(true); | |
final Button btnClear = new Button("clearName(OK)"); | |
btnClear.setDisable(true); | |
final Button btnNotClear = new Button("clearName(cancel)"); | |
btnNotClear.setDisable(true); | |
engine.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() { | |
@Override | |
public void changed(ObservableValue<? extends State> ov, State oldState, State newState) { | |
if (newState == State.SUCCEEDED && engine.getTitle().equalsIgnoreCase("Hello")) { | |
btnHello.setDisable(false); | |
btnMorning.setDisable(false); | |
btnClear.setDisable(false); | |
btnNotClear.setDisable(false); | |
} else { | |
btnHello.setDisable(true); | |
btnMorning.setDisable(true); | |
btnClear.setDisable(true); | |
btnNotClear.setDisable(true); | |
} | |
} | |
}); | |
HBox hbox = new HBox(); | |
hbox.setPadding(new Insets(15,12,15,12)); | |
hbox.setSpacing(10); | |
hbox.setStyle("-fx-background-color: #336699;"); | |
hbox.getChildren().addAll(btnLoad, btnHello, btnMorning, btnClear, btnNotClear); | |
box.getChildren().add(hbox); | |
btnLoad.setOnAction(getLoadAction()); | |
btnHello.setOnAction(getHelloAction()); | |
btnMorning.setOnAction(getおはようAction()); | |
btnClear.setOnAction(getClearNameAction(true)); | |
btnNotClear.setOnAction(getClearNameAction(false)); | |
engine.setConfirmHandler(getConfirmAction()); | |
stage.setScene(new Scene(box, 800, 300)); | |
stage.show(); | |
} | |
private EventHandler<ActionEvent> getLoadAction() { | |
return new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent t) { | |
engine.load("http://localhost:8080/HelloSample"); | |
} | |
}; | |
} | |
private EventHandler<ActionEvent> getHelloAction() { | |
return new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent t) { | |
engine.executeScript("document.getElementById('form1:name').value = 'JavaFX'"); | |
engine.executeScript("document.getElementsByName('form1:greetSel')[0].checked = true"); | |
engine.executeScript("document.getElementById('form1:language').options[0].selected = true"); | |
engine.executeScript("var btn = document.getElementById('form1:greet'); btn.click();"); | |
} | |
}; | |
} | |
private EventHandler<ActionEvent> getおはようAction() { | |
return new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent t) { | |
engine.executeScript("document.getElementById('form1:name').value = 'JavaFX'"); | |
engine.executeScript("document.getElementsByName('form1:greetSel')[1].checked = true"); | |
engine.executeScript("document.getElementById('form1:language').options[1].selected = true"); | |
engine.executeScript("var btn = document.getElementById('form1:greet'); btn.click();"); | |
} | |
}; | |
} | |
private EventHandler<ActionEvent> getClearNameAction(final boolean isOK) { | |
return new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent t) { | |
isConfirmOK = isOK; | |
engine.executeScript("var btn = document.getElementById('form1:clearButton'); btn.click();"); | |
} | |
}; | |
} | |
private Callback<String, Boolean> getConfirmAction() { | |
return new Callback<String, Boolean>() { | |
@Override | |
public Boolean call(String p) { | |
if (isConfirmOK) { | |
return true; | |
} | |
return false; | |
} | |
}; | |
} | |
public static void main(String... args) { | |
launch(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment