I'm building a Swing application and I'm using JFXPanel to bring up a WebView for user authentication. I need to return the url that the WebEngine is sent to after a successful user sign-on- but I don't know how to pull the URL from the view at the time the window is closed. I'm aware of Stage.setOnHiding, but this can't apply here since JFXPanels don't act as stages, though they contain a scene.
I'm very new to JavaFX so if this code snippet demonstrates bad conventions, I'd be happy to hear it!
private static void buildAuthBrowser() {
JFrame frame = new JFrame("frame");
final JFXPanel fxPanel = new JFXPanel();
frame.add(fxPanel);
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Platform.runLater(new Runnable() {
#Override
public void run() {
initFX(fxPanel);
}
});
}
private static void initFX(JFXPanel fxPanel) {
// fx thread
Scene scene = createScene();
fxPanel.setScene(scene);
}
private static Scene createScene() {
Group root = new Group();
Scene scene = new Scene(root);
WebView view = new WebView();
WebEngine browser = view.getEngine();
browser.load(auth.getUrl());
root.getChildren().add(view);
return (scene);
}
Found a solution- I was able to set a listener on the engine in the scene builder method, this way, every time the browser visits a new URL it automatically updates a private variable.
in createScene():
authBrowser.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {
if (Worker.State.SUCCEEDED.equals(newValue)) {
setCode(authBrowser.getLocation());
}
});
Related
I can't figure out how to create a modal window in JavaFX. Basically I have file chooser and I want to ask the user a question when they select a file. I need this information in order to parse the file, so the execution needs to wait for the answer.
I've seen this question but I've not been able to find out how to implement this behavior.
In my opinion this is not good solution, because parent window is all time active.
For example if You want open window as modal after click button...
private void clickShow(ActionEvent event) {
Stage stage = new Stage();
Parent root = FXMLLoader.load(
YourClassController.class.getResource("YourClass.fxml"));
stage.setScene(new Scene(root));
stage.setTitle("My modal window");
stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner(
((Node)event.getSource()).getScene().getWindow() );
stage.show();
}
Now Your new window is REALY modal - parent is block.
also You can use
Modality.APPLICATION_MODAL
Here is link to a solution I created earlier for modal dialogs in JavaFX 2.1
The solution creates a modal stage on top of the current stage and takes action on the dialog results via event handlers for the dialog controls.
JavaFX 8+
The prior linked solution uses a dated event handler approach to take action after a dialog was dismissed. That approach was valid for pre-JavaFX 2.2 implementations. For JavaFX 8+ there is no need for event handers, instead, use the new Stage showAndWait() method. For example:
Stage dialog = new Stage();
// populate dialog with controls.
...
dialog.initOwner(parentStage);
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.showAndWait();
// process result of dialog operation.
...
Note that, in order for things to work as expected, it is important to initialize the owner of the Stage and to initialize the modality of the Stage to either WINDOW_MODAL or APPLICATION_MODAL.
There are some high quality standard UI dialogs in JavaFX 8 and ControlsFX, if they fit your requirements, I advise using those rather than developing your own. Those in-built JavaFX Dialog and Alert classes also have initOwner and initModality and showAndWait methods, so that you can set the modality for them as you wish (note that, by default, the in-built dialogs are application modal).
You can create application like my sample. This is only single file JavaFX application.
public class JavaFXApplication1 extends Application {
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Stage stage;
stage = new Stage();
final SwingNode swingNode = new SwingNode();
createSwingContent(swingNode);
StackPane pane = new StackPane();
pane.getChildren().add(swingNode);
stage.initModality(Modality.APPLICATION_MODAL);
stage.setTitle("Swing in JavaFX");
stage.setScene(new Scene(pane, 250, 150));
stage.show();
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
private void createSwingContent(final SwingNode swingNode) {
SwingUtilities.invokeLater(() -> {
try {
Path currentRelativePath = Paths.get("");
String s = currentRelativePath.toAbsolutePath().toString();
JasperDesign jasperDesign = JRXmlLoader.load(s + "/src/reports/report1.jrxml");
String query = "SELECT * FROM `accounttype`";
JRDesignQuery jrquery = new JRDesignQuery();
jrquery.setText(query);
jasperDesign.setQuery(jrquery);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint JasperPrint = JasperFillManager.fillReport(jasperReport, null, c);
//JRViewer viewer = new JRViewer(JasperPrint);
swingNode.setContent(new JRViewer(JasperPrint));
} catch (JRException ex) {
Logger.getLogger(AccountTypeController.class.getName()).log(Level.SEVERE, null, ex);
}
});
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
I'm working on an application which is based on swing and javafx 8. In this create a frame in swing and jbutton use and jbutton action code is done in javafx 8 means use scene. in this a alert dialog create.but problem is that if i click anywhere except alert dialog then alert dialog hidden behind swing frame.i want javafx alert dialog keep on swing frame which i create.if i click ok on alert then action is goes to swing frame.
please suggest......
here is my code:
final JFXPanel fxPanel = new JFXPanel();
Platform.runLater(new Runnable() {
public void run() {
initFX(fxPanel);
}
private void initFX(JFXPanel fxPanel) {
// TODO Auto-generated method stub
Scene scene = createScene();
fxPanel.setScene(scene);
}
private Scene createScene() {
Group root = new Group();
Scene scene = new Scene(root);
Alert cn=new Alert(AlertType.CONFIRMATION);
cn.initStyle(StageStyle.UNDECORATED);
cn.setTitle(null);
cn.setHeaderText(null);
cn.setContentText(ProjectProps.rb.getString("msg.play.confirm"));
DialogPane dp=cn.getDialogPane();
dp.getStylesheets().add(getClass().getResource("login.css").toExternalForm());
dp.setStyle("-fx-border-color: black; -fx-border-width: 5px; ");
Optional<ButtonType> result=cn.showAndWait();
if(result.get()==ButtonType.OK){
System.out.println("ok button clicked:"+result.get());
k=0;
} else{
System.out.println("cancel clicked");
k=1;
}
return (scene);
}
});
I'm not sure to understand what you really want to do. Do you absolutely need a JavaFX alert in a swing application? JDialog works very well, and Alert and Dialogs works very well too in a JavaFX context.
Have you tried to set your Alert application modal with cn.initModality(Modality.APPLICATION_MODAL) ?
Maybe you can find you answer in this post : How to make a JFrame Modal in Swing java
This is the suggested code in the post:
final JDialog frame = new JDialog(parentFrame, frameTitle, true);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
I would create a new JDialog and set as parameter your main frame (parentFrame) and add your JFXPanel to its content.
frame.getContentPane.add(fxPanel)
You can use fxPanel.setScene(yourScene) to add FX content to your JDialog.
Example for the suggestion:
public class Main {
public static void main(String[] args) {
// create parent
JFrame parent = new JFrame("MainFrame");
parent.setSize(500, 500);
// center of screen
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
parent.setLocation(dim.width/2-parent.getSize().width/2, dim.height/2-parent.getSize().height/2);
parent.setVisible(true);
createDialog(parent);
}
private static void createDialog(JFrame parent) {
// create dialogs JFX content
BorderPane layout = new BorderPane();
layout.setTop(new Button("HELLO"));
layout.setBottom(new Button("IT'S ME"));
Scene scene = new Scene(layout);
JFXPanel dlgContent = new JFXPanel();
dlgContent.setScene(scene);
dlgContent.setPreferredSize(new Dimension(200, 200));
dlgContent.setVisible(true);
// create dialog and set content
JDialog dlg = new JDialog(parent, "Dialog", true);
dlg.setLocationRelativeTo(parent);
dlg.getContentPane().add(dlgContent);
dlg.pack();
dlg.setVisible(true);
}
}
Hope I could help you.
I have a JPanel component containing a JFXPanel with a browser that embedds a YouTube video. I'm using the videos embed URL from YouTube (i.e. https://www.youtube.com/embed/W-J2OYN9fF8?autoplay=true&controls=0).
I can add the JPanel (VideoPlayer) to a surrounding component without any problem. However - when I remove the VideoPlayer I would also like to stop the YouTube-player. As of now - it keeps playing in the background (with annoying sound). I'm guessing I have to get inside the JFX thread somehow... So, if someone could please help me with code to put in the stopTrailer() method - I'd be very grateful!
Here's my current code. For those who are looking for a simple way to embed a JFX YouTube player in a normal JPanel - this works great - appart from the limitations above...
public class VideoPlayer extends JPanel {
private Stage stage;
private WebView browser;
private JFXPanel jfxPanel;
private WebEngine webEngine;
private String videoUrl;
public VideoPlayer(String url){
this.videoUrl = url;
jfxPanel = new JFXPanel();
createScene();
setLayout(new BorderLayout());
setPreferredSize(new Dimension(800, 560));
add(jfxPanel, BorderLayout.CENTER);
}
private void createScene() {
PlatformImpl.startup(new Runnable() {
#Override
public void run() {
stage = new Stage();
stage.setTitle("Video");
stage.setResizable(true);
Group root = new Group();
Scene scene = new Scene(root,80,20);
stage.setScene(scene);
//Set up the embedded browser:
browser = new WebView();
webEngine = browser.getEngine();
webEngine.load(videoUrl);
ObservableList<Node> children = root.getChildren();
children.add(browser);
jfxPanel.setScene(scene);
}
});
}
public void stopTrailer() {
}
}
The answer to this was actually really simple. I put the following in the stopTrailer method.
public void stopTrailer() {
PlatformImpl.startup(new Runnable() {
#Override
public void run() {
remove(jfxPanel);
webEngine.load(null);
}
});
}
What it actually does is to reload the browser webEngine to the "address" null. And removes the panel. It seems a bit weird to me cause I would interpret this code as the start of a second thread - without access to the first one. Perhaps someone could explain just how this works?
I am trying to instantiate many JFrames with WebView, it will work as long as after opening the first WebView, at least one WebView is still alive and the JFrame (along with everything else) arent disposed of.
After some checking, it seems that after closing the all WebView JFrame, the Platform.runLater() no longer runs on the next instantiation of the JFrame with the WebViewPanel.
The following is the simplified code:
public class WebViewPanel{
private JFXPanel jfxPanel;
private WebView view;
private WebEngine webEngine;
private JPanel panel;
public WebViewPanel(JPanel panel){
this.panel = panel;
panel.setVisible(true);
createScene();
}
private void createScene(){
Platform.runLater(new Runnable(){
#Override
public void run(){ //Runnable no longer runs after all JFrames with WebViewPanels are closed
jfxPanel = new JFXPanel();
view = new WebView();
webEngine = view.getEngine();
jfxPanel.setScene(new Scene(view));
panel.add(jfxPanel);
jfxPanel.setVisible(true);
}
});
}
}
So, what must I do to resolve this problem?
This happens because Platform exits itself if not explicitly set.
Here's what you need to do just after
Platform.runLater(new Runnable(){ // ..code
add Platform.setImplicitExit(false); at the first row
so your new code will be -
private void createScene(){
Platform.runLater(new Runnable(){
Platform.setImplicitExit(false);
#Override
public void run(){
jfxPanel = new JFXPanel();
view = new WebView();
webEngine = view.getEngine();
jfxPanel.setScene(new Scene(view));
panel.add(jfxPanel);
jfxPanel.setVisible(true);
}
});
}
Just FYI if you are trying to display the panel more than once there is no need to call createScene() add the JFXPanel to a JFrame(preferable panel works fine too) with a static reference. dispose the frame to close it and when again you need to show this simply set the same frame setVisibale(true);. everytime calling this frame you can also check if the static reference of this frame points to null(representing first call) then call createscene() else just set the static reference to visible.
I try to embed a JavaFX panel to java Swing using the tutorial from here
I am using java 8 update 20, JavaFX 2.0 or 2.2 (not very sure how to check)
Here is my code:
JavaFX code:
public class SwingFXWebView{
public final JFXPanel jfxpanel;
public SwingFXWebView(String[] para, int width, int height){
jfxpanel = new JFXPanel();
jfxpanel.setLayout(new BorderLayout());
//add(jfxpanel, BorderLayout.CENTER);
Platform.runLater( new Runnable(){
public void run(){
initFX(jfxpanel);
}
});
}
private static void initFX(JFXPanel jfxpanel){
Scene scene = createScene();
jfxpanel.setScene(scene);
}
private static Scene createScene() {
Platform.setImplicitExit(false);
Group root = new Group();
final Scene scene = new Scene(root, Color.ALICEBLUE);
WebView browser;
browser = new WebView();
//browser.setContextMenuEnabled(false);
browser.autosize();
WebEngine webEngine = browser.getEngine();
webEngine.load("www.google.ca");
root.getChildren().add(browser);
return (scene);
}
Swing code:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Jdesktop browser
sfwv = new SwingFXWebView(para, panelContents.getWidth(),panelContents.getHeight());
panelContents.add(sfwv.jfxpanel, BorderLayout.CENTER);
....
}
It works so far. if I mouse right click to refresh the page and do it for second time, the JVM crashes.
A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000000005c2728e0, pid=5376, tid=5084
#
It looks like the thread problem, but I strictly follow the tutorial from oracle. Any idea what I have done wrong?
Thanks in advance!