Vikash Anand
Vikash Anand

Reputation: 455

Display HTML String in a Browser

I am working on a project and have embedded a JavaFx Browser in a java application. Now, I would like to display some contents in the Browser.

I wanted to know that is there a way to display the contents through a string that contains HTML tags. I do not want to create a HTML file and the display the contents.

I was looking for something like JTextPane.

Upvotes: 2

Views: 4535

Answers (3)

browsermator
browsermator

Reputation: 1354

When creating the "Scene" and adding it to the JFXPanel you want to put it in the JavaFX thread by using Platform.runLater :

 import javafx.application.Platform;
 import javafx.embed.swing.JFXPanel;

    String HTML_TO_SEND = GetHTML();

    Platform.runLater(new Runnable() {
        @Override
        public void run() {
  browser = new WebView();
  webEngine = browser.getEngine();
  webEngine.loadContent(HTML_TO_SEND);

  browser.setVisible(true);
  Scene scene = new Scene(browser);
    jfxPanel.setScene(scene);


        }
  });   

You can then add the jfxPanel to your Swing stuff as usual.

Upvotes: 0

Uluk Biy
Uluk Biy

Reputation: 49215

I could not investigate that JavaFX Browser Netbeans Module in depth. But if it is using a JavaFX WebView control internally then you can load String value containing HTML page (or tag) via WebView's WebEngine. See the WebEngine API for different content loadings. That Netbeans Module should have some interface API allowing this.

The method you want is webView.getEngine().loadContent(htmlString)

Upvotes: 6

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51565

You have to change the < to &lt, followed by a semicolon, and change the > to &gt, followed by a semicolon.

Just like I did to type this answer.

Upvotes: 1

Related Questions