Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to apply my custom css rule to a JavaFX Web Browser?

1037608Sep 30 2013 — edited Oct 4 2013

I want to apply my css rule to specific nodes of WebView.

I use  Node.lookupAll(String cssSelector) method to find the node

(http://docs.oracle.com/javafx/2/api/javafx/scene/Node.html#lookupAll%28java.lang.String%29)

and Node.setStyle(String ccsStyle)  to apply my rules  (http://docs.oracle.com/javafx/2/api/javafx/scene/Node.html#setStyle%28java.lang.String%29)

As an example I use the following code from JavaFX Documentation

http://docs.oracle.com/javafx/2/webview/jfxpub-webview.htm

public class WebViewSample extends Application {

    private Scene scene;

    @Override

    public void start(Stage stage) {      

        stage.setScene(scene);

        stage.show();

    }

    public static void main(String[] args) {

        launch(args);

    }

}

class Browser extends Region {

    final WebView browser = new WebView();

    final WebEngine webEngine = browser.getEngine();

    public Browser() {

     

        webEngine.load("http://www.google.gr");

        getChildren().add(browser);

        Set<Node> lookupAll = browser.lookupAll("body");

        if (lookupAll.isEmpty()) {

            System.out.println("Lookup is empty");

        } else {

            for (Node n : lookupAll) {

                n.setStyle("{background-color:gray;}");

            }

        }

    }

    private Node createSpacer() {

        Region spacer = new Region();

        HBox.setHgrow(spacer, Priority.ALWAYS);

        return spacer;

    }

    @Override

    protected void layoutChildren() {

        double w = getWidth();

        double h = getHeight();

        layoutInArea(browser, 0, 0, w, h, 0, HPos.CENTER, VPos.CENTER);

    }

    @Override

    protected double computePrefWidth(double height) {

        return 750;

    }

    @Override

    protected double computePrefHeight(double width) {

        return 500;

    }

}

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 1 2013
Added on Sep 30 2013
2 comments
578 views