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("");
Related
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();
}
}
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;
}
We are using more than one stage(window) in JavaFX for a desktop standalone application. So when we drag or move any of the stage(window) form one point to another and clicking a certain button to navigate to the other page means the new stage is coming to center again(default position). Is there any methods to set the future stage(future windows) also in the previous stage location or point(previous windows). Please help.
You can control stage location before showing it. Just remember last stage coordinates and create new one in the same point:
public class FXStages extends Application {
private int counter = 0;
#Override
public void start(final Stage stage) {
Button btn = new Button();
btn.setText("Reopen");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
stage.close();
Stage newStage = new Stage();
newStage.setX(stage.getX());
newStage.setY(stage.getY());
start(newStage);
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
stage.setTitle("Stage " + counter++);
stage.setScene(scene);
stage.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();}