Skip to content

Instantly share code, notes, and snippets.

@aoetk
Created April 16, 2015 09:54
Show Gist options
  • Save aoetk/1221f5942ab15f90c714 to your computer and use it in GitHub Desktop.
Save aoetk/1221f5942ab15f90c714 to your computer and use it in GitHub Desktop.
ローカルのWebサーバに置いたFirebug LiteをJavaFXのWebViewで使うサンプル
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class FirebugSampleApp extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("FirebugSampleView.fxml"));
primaryStage.setTitle("Firebug Sample");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.web.WebView?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="800.0" prefWidth="720.0"
xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="sample.FirebugSampleViewController">
<top>
<HBox spacing="10.0" BorderPane.alignment="CENTER">
<children>
<TextField fx:id="locationField" HBox.hgrow="ALWAYS" />
<Button defaultButton="true" mnemonicParsing="false" text="Go" onAction="#handleGoButtonAction" />
<Button mnemonicParsing="false" text="Firebug" onAction="#handleFirebugButtonAction" />
</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</HBox>
</top>
<center>
<WebView fx:id="webView" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
package sample;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class FirebugSampleViewController implements Initializable {
@FXML
private TextField locationField;
@FXML
private WebView webView;
private WebEngine webEngine;
@Override
public void initialize(URL location, ResourceBundle resources) {
webEngine = webView.getEngine();
}
public void handleGoButtonAction(ActionEvent event) {
webEngine.load(locationField.getText());
}
public void handleFirebugButtonAction(ActionEvent event) {
if (webEngine.getDocument() != null) {
String firebugLiteUrl = "http://localhost:8080/firebug-lite/build/firebug-lite.js#startOpened";
Document document = webEngine.getDocument();
Element scriptElement = document.createElement("script");
scriptElement.setAttribute("type", "text/javascript");
scriptElement.setAttribute("src", firebugLiteUrl);
NodeList bodyList = document.getElementsByTagName("body");
if (bodyList != null && bodyList.getLength() > 0) {
bodyList.item(0).appendChild(scriptElement);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment