Thursday, May 17, 2012

Preview HTMLEditor content in WebView

We can load content of HTMLEditor in WebView using loadContent() method of WebEngine.

Preview HTMLEditor content in WebView


package javafx_html;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.VBox;
import javafx.scene.layout.VBoxBuilder;
import javafx.scene.web.*;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_HTMLeditor extends Application {
 

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(final Stage primaryStage) {
        primaryStage.setTitle("java-buddy.blogspot.com");
        Group root = new Group();
        
        final HTMLEditor htmlEditor = HTMLEditorBuilder.create()
                .prefHeight(200)
                .prefWidth(400)
                .build();

        final WebView webView = WebViewBuilder.create()
                .prefWidth(400)
                .build();
        
        final WebEngine webEngine = webView.getEngine();
        
        ScrollPane scrollPane = new ScrollPane();
        scrollPane.getStyleClass().add("noborder-scroll-pane");
        scrollPane.setContent(webView);
        scrollPane.setFitToWidth(true);
        scrollPane.setPrefHeight(180);
        
        Button buttonPreview = new Button("Preview");
        buttonPreview.setOnAction(new EventHandler<ActionEvent>(){

            @Override
            public void handle(ActionEvent t) {
                webEngine.loadContent(htmlEditor.getHtmlText());
            }
        });

        VBox vBox = VBoxBuilder.create()
                .children(htmlEditor, scrollPane, buttonPreview)
                .build();
        
        root.getChildren().add(vBox);
        primaryStage.setScene(new Scene(root, 500, 400));
        primaryStage.show();
    }
    
}


Related:
- Save and Load HTMLEditor in file

No comments:

Post a Comment