how to open pop up in new window using javafx web view - java

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();}

Related

How to close an embedded JavaFX Application in Swing [duplicate]

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("");

I have a button in my index.html ,open that html and want to perform on click action using the button and redirect it to different page

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();
}
}

JavaFX with Swing

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;
}

browser new window (window.open) can't load using javafx

engine.setCreatePopupHandler(new Callback<PopupFeatures, WebEngine>() { // todo should create a new tab.
#Override public WebEngine call(PopupFeatures popupFeatures) {
Stage stage = new Stage(StageStyle.UTILITY);
WebView wv2 = new WebView();
engine.load(getView().getEngine().getLocation());
stage.setScene(new Scene(wv2));
stage.setX(stage.getX() + 250);
stage.setY(stage.getY() + 100);
stage.initModality(Modality.APPLICATION_MODAL);
stage.show();
System.out.println("Popup: " + popupFeatures);
return wv2.getEngine();
}
});
Above is my code.
I made my own browser it's all functionality working and through this code
new window is open but the web page functionality is not working on new engine.
how can i do it again and again create new engine dynamically.
Please help me and sorry for poor english.
Thank you

connecting two web view with hyperlink event in javafx

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.

Categories

Resources