I am trying to connect two classes. One is main swing class and other is javafx web view in a swing frame. This is run perfectly but do not show the exact output. My URL is coming from my swing frame class.
public Instruction() {
System.out.println("new url number is=="+newurl);
System.out.println("token number is=="+first.tokennum);
System.out.println("final link is="+FirstToken.finallink);
frame1.add(panel);
frame1.setSize(500,500);
frame1.setVisible(true);
Platform.runLater(new Runnable() {
#Override
public void run() {
//To change body of generated methods, choose Tools | Templates.
initfx(panel);
}
});
}
public void initfx(JFXPanel panel){
Scene scene = createscene();
panel.setScene(scene);
geturl(FirstToken.finallink);
}
public Scene createscene(){
Group root = new Group();
Scene scene=new Scene(root);
Text text = new Text();
text.setX(50);
text.setY(100);
root.getChildren().add(text);
return (scene);
}
public void geturl(String url){
WebView web=new WebView();
System.out.println("url is="+url);
web.getEngine().load(url);
}
You are not adding the WebView that you create to the scene graph. To fix it, have your geturl routine return your WebView and add that WebView to the group which forms the root of your scene.
If you don't understand, read the Oracle tutorial on integrating a JavaFX WebView into Swing and review the accompanied source.
Example code:
public WebView geturl(String url){
WebView webView = new WebView();
webView.getEngine().load(url);
return webView;
}
public void initfx(JFXPanel panel){
WebView webView = geturl(FirstToken.finallink);
Scene scene = createscene(webView);
panel.setScene(scene);
}
public Scene createscene(WebView webView){
Pane root = new Pane();
Scene scene = new Scene(root);
root.getChildren().add(webView);
return scene;
}
Related
When i create new stage with WebEngine that playing video from YouTube, after i close it - Youtube keeps playing on backgroung. If i use "Platform.exit" - its close all my JavaFX App, but i want to close only stage that been created for YouTube.
This is my class for YouTube player:
public class YouTube_player {
public YouTube_player(String url) {
final Group root = new Group();
Scene scene = new Scene(root, 820, 480);
final Stage stage = new Stage();
final WebView webView = new WebView();
final WebEngine webEngine = webView.getEngine();
webEngine.loadContent(url);
root.getChildren().add(webView);
stage.centerOnScreen();
stage.setScene(scene);
stage.show();
stage.setOnCloseRequest(new EventHandler<WindowEvent>(){
#Override
public void handle(WindowEvent event) {
//What i should put here to close only this stage.
//Platform.exit - closes all my stages.
//webEngine.getLoadWorker().cancel(); - dont stop Youtube )))
}
});
}
}
My Youtube player stage is creating after i clicking on button in 'mainstage':
b1.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent event) {
new YouTube_player(url_video_p1);
}
});
You cant dispose the webengine the only thing that you can do is to set the content of the webEngine to null
webView.getEngine().load(null);
Java 9, for me works:
webView.getEngine().load("");
If the button inside index.html has id, then how to call onclick on that button inside java application using that id.
Here is the Java Code:
public class Abcd extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("fdg");
StackPane root = new StackPane();
webView = new WebView();
webEngine = webView.getEngine();
URL urlHello = getClass().getResource("/2/index.html");
webEngine.load(urlHello.toExternalForm());
root.getChildren().add(webView);
scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Hi i'm using JavaFx WebView to create Screenshot of HTML pages and it works fine but i wanted to know is it possible to do this without launching the application in Graphical Windows!! I mean aren't there any more lightweight method to get the screenshot then this:
public class WebViewSample extends Application {
private Scene scene;
#Override
public void start(Stage stage) {
// create scene
scene = new Scene(new Browser(snapshot), 750, 500, Color.web("#666970"));
stage.setScene(scene);
// show stage
stage.show();
}
WritableImage snapshot;
public static void main(String[] args) {
launch(args);
System.err.println("launched!");
}
}
class Browser extends Region {
final ImageView selectedImage = new ImageView();
final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
private final WritableImage snapshotImage;
public Browser(WritableImage snapshot) {
this.snapshotImage= snapshot;
// process page loading
webEngine.getLoadWorker().stateProperty().addListener(
new ChangeListener<State>() {
#Override
public void changed(ObservableValue<? extends State> ov,
State oldState, State newState) {
if (newState == State.SUCCEEDED) {
WritableImage newSnapshot = browser.snapshot(null, snapshotImage);
File file = new File("test2.png");
RenderedImage renderedImage = SwingFXUtils.fromFXImage(newSnapshot, null);
try {
ImageIO.write(renderedImage, "png", file);
} catch (IOException e1) {
e1.printStackTrace();
}
System.exit(0);
}
}
}
);
// load the home page
webEngine.load("http://localhost/");
//add components
getChildren().add(browser);
}
}
For JavaFX 2.2 and below, there's not functionality of the kind.
Headless JavaFX applications are currently not possible, and a main JavaFX thread is mandatory.
The best you can do is read upon several workarounds for achieving this.
Related StackOverflow questions:
JavaFX for server-side image generation
Generating image at server side using Java FX
JavaFx in headless mode
How to test JavaFX 2 in a headless environment?
I am trying to implement popup handler in javafx webview its generating the popup and adding it on the same fxpanel but i want to display it onto the new window . I am using the following code.
try
{
Webview webview,smallView
final Group group= new Group();
Scene scene= new Scene(group);
fxpanel.setScene(scene);
webview = new WebView ();
group.getChildren().add(webview);
eng= webview.getEngine();
String url ="http://www.timetrim.in/webchatserver2/default.aspx";
eng.load(url);
smallView = new WebView();
smallView.setPrefSize(400, 200);
eng.setCreatePopupHandler(
new Callback<PopupFeatures, WebEngine>() {
#Override
public WebEngine call(PopupFeatures config) {
smallView.setFontScale(0.8);
if (!group.getChildren().contains(smallView)) {
group.getChildren().add(smallView);
}
return smallView.getEngine();
}
});
}
catch(Exception ex){ex.printStackTrace();}
I am trying to open webpage in another webview by using following code. In this program i am using two webview in one test1.htm is displayed and i want on clicking the hyperlink in first webview the new page should open in another webview
class webcontrol extends JFrame
{
webcontrol()
{
setLayout(null);
Container cp=this.getContentPane();
fxpanel= new JFXPanel();
add(fxpanel);
fxpanel.setBounds(660, 0, 230, screenSize.height-120);
Platform.runLater(new Runnable() {
public void run()
{
initFx(fxpanel,fxpanel1);
}
}
);
}
private static void initFx(final JFXPanel fxpanel, JFXPanel fxpanel1)
{
WebView webview=null,webview1=null;
WebEngine eng=null,eng1=null;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Group group1= new Group();
Scene scene1= new Scene(group1);
fxpanel1.setScene(scene1);
webview1 = new WebView ();
group1.getChildren().removeAll();
group1.getChildren().add(webview1);
Group group= new Group();
Scene scene= new Scene(group);
fxpanel.setScene(scene);
webview = new WebView ();
group.getChildren().removeAll();
group.getChildren().add(webview);
eng= webview.getEngine();
eng1=webview1.getEngine();
File htmlFile = new File("d:/newfolder/test.htm");
File htmlFile1 = new File("d:/newfolder/test1.htm");
try
{
eng.load(htmlFile.toURI().toURL().toString());
eng1.load(htmlFile1.toURI().toURL().toString());
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
There is no closing of String in:
("d:/newfolder/test1.htm);
Where as
("d:/newfolder/test.htm");
is fine.