package javafx_htmleditor; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; import javafx.application.Application; import javafx.application.Platform; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.ScrollPane; import javafx.scene.control.TextArea; import javafx.scene.layout.VBox; import javafx.scene.web.HTMLEditor; import javafx.stage.Stage; /** * * @web http://java-buddy.blogspot.com/ */ public class JavaFX_HTMLeditor extends Application { @Override public void start(Stage primaryStage) { primaryStage.setTitle("java-buddy.blogspot.com"); Group root = new Group(); HTMLEditor htmlEditor = new HTMLEditor(); htmlEditor.setPrefHeight(300); TextArea htmlText = new TextArea(); htmlText.setWrapText(true); ScrollPane scrollPane = new ScrollPane(); scrollPane.setContent(htmlText); scrollPane.setFitToWidth(true); scrollPane.setPrefHeight(180); VBox vBox = new VBox(); vBox.getChildren().addAll(htmlEditor, scrollPane); root.getChildren().add(vBox); primaryStage.setScene(new Scene(root, 850, 500)); primaryStage.show(); LoadPageThread loadPageThread = new LoadPageThread("http://java-buddy.blogspot.com", htmlEditor, htmlText); loadPageThread.start(); } public static void main(String[] args) { launch(args); } class LoadPageThread extends Thread { String PageSrc; HTMLEditor editor; TextArea textArea; public LoadPageThread(String src, HTMLEditor editor, TextArea textArea) { PageSrc = src; this.editor = editor; this.textArea = textArea; } @Override public void run() { String result = loadPage(PageSrc); Platform.runLater(() -> { //update html code in HTMLEditor editor.setHtmlText(result); //get html code from HTMLEditor String html = editor.getHtmlText(); textArea.setText(html); }); } private String loadPage(String src) { String pageCode = ""; try { URL url = new URL(src); URLConnection connection = url.openConnection(); InputStream inputStream = connection.getInputStream(); Scanner scanner = new Scanner(inputStream); while (scanner.hasNextLine()) { pageCode += scanner.nextLine() + "\n"; } } catch (MalformedURLException ex) { Logger.getLogger(JavaFX_HTMLeditor.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(JavaFX_HTMLeditor.class.getName()).log(Level.SEVERE, null, ex); } return pageCode; } } }
Tuesday, January 20, 2015
JavaFX 8 HTMLEditor example, load content from URL
It's a example of JaqvaFX HTMLEditor, its content is loaded from internet using URLConnection on another Thread.
No comments:
Post a Comment