It says on the OpenJDK samples for TextFlow that it can have arbitrary Nodes as children, e.g. Button. I was wondering if WebView can be a possible child? If so, then I must be doing something wrong. When I add it to the TextFlow, the WebView just takes up space, but displays nothing. The content I've tried range from simple HTML pages, up to videos from services like YouTube.
Since this control is fairly new, I was unable to find an existing use case online.
In case you're wondering why I want this. I have a ListView that displays rich content. If TextFlow is capable of displaying any Node, then it would save me from creating multiple extension classes for the different types I want to display on the ListView (e.g. Video, formatted text, HTML - these I already did, and they work fine), and I can manage content easily by just managing the TextFlow's children.
Thanks.
Yes, you can. Note that the web view will be layout out using its baselineOffset
, which is not going to be appropriate for use in a TextFlow
(I think what happens is it's bottom edge will be aligned with the bottom edge of the text, and it gets clipped to the height of the text row.) You can get around this by wrapping it in a Group
:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class WebViewInTextFlow extends Application {
@Override
public void start(Stage primaryStage) {
TextFlow flow = new TextFlow();
WebView webView = new WebView();
webView.getEngine().getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> System.out.println(newState));
webView.getEngine().load("http://stackoverflow.com/questions/31353921/is-webview-embeddable-in-textflow/31357609#31357609");
flow.getChildren().addAll(new Text("Some text"), new Group(webView), new Text("Some more text"));
primaryStage.setScene(new Scene(flow, 800, 800));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}