I am working on embedding a JavaFX login screen inside a Swing application.
I added a JFXPanel to a JPanel's content frame. Upon loading the application, all is well and smooth until I move my mouse inside the content pane (see link below for a video of this happening). While my mouse is actively moving in the pane, the rendering becomes very laggy:
Desired behaviour:
- Use JavaFX designed UI in Swing through means of a JFXPanel without suffering a significant decrease in performance
The code below is a minimal, reproducible example of this problem.
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.*;
import javax.swing.*;
import java.io.IOException;
import java.net.URL;
public class TestJFXPanel {
public static void main(String[] args) throws IOException {
/*
Create pane with as background an animated image (GIF)
*/
final Pane pane = new Pane();
final URL url = new URL("https://i.stack.imgur.com/AvkzQ.gif");
pane.setBackground(new Background(new BackgroundImage(
new Image(url.openStream()),
BackgroundRepeat.NO_REPEAT,
BackgroundRepeat.NO_REPEAT,
BackgroundPosition.CENTER,
BackgroundSize.DEFAULT)));
/*
Set the pane as root of a Scene
*/
final JFXPanel panel = new JFXPanel();
panel.setScene(new Scene(pane, 500, 500));
/*
Invoke JFrame on AWT thread
*/
SwingUtilities.invokeLater(() -> {
final JFrame jFrame = new JFrame();
// Add JFXPanel to content pane of JFrame
jFrame.getContentPane().add(panel);
jFrame.pack();
jFrame.setVisible(true);
});
}
}
My thoughts:
- I suspect this has something to do with the mouse motion being redirected to Swing from JavaFX causing a delay in communication between the FX and Swing threads. I think this is especially costly because I use a GIF as background image of the login screen.
- I am not sure whether this is caused by a delay in communication of FX and Swing, if it is the case, would handling all my FX UI in Swing resolve this (assuming this is more feasible than porting the entire design to Swing)?
However, if someone is more familiar with integrating JavaFX with Swing and thinks my suspicions are wrong. Some advice on how to proceed are much appreciated!
Update:
- When I override the
processMouseMotionEvent
method of the JFXPanel class, so that no MouseMotionEvents get consumed, the problem does not occur. I think this might be a compatibility issue between JavaFX and MacOS.