JavaFX and Swing performance issues - java

We have been having perpetual performance issues when running JavaFX inside a JFXPanel in Swing based applications.
This seems to only be a problem when running on JDK1.7, because whenever it is possible to run JDK1.8 this works perfectly without changing any code.
The symptoms are that the application seems to render fonts in a fuzzy way and also the performance is terrible (multiple seconds to respond to keypress when typing in a TextField).
We are observing the correct rules about EDT, AWT and Platform threads, so I doubt that this can be the issue.
We are stuck having to support JDK1.7 because this is a plug-in for NetBeans which some users will be running on JDK1.7 for various good reasons and we cannot force them to upgrade.
EDIT: Here is a MCVE which recreates the problem
package javaapplication3;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Test {
private static void initAndShowGUI() {
// This method is invoked on the EDT thread
JFrame frame = new JFrame("Swing and JavaFX");
final JFXPanel fxPanel = new JFXPanel();
frame.add(fxPanel);
frame.setSize(300, 200);
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) {
// This method is invoked on the JavaFX thread
Scene scene = createScene();
fxPanel.setScene(scene);
}
private static Scene createScene() {
Group root = new Group();
Scene scene = new Scene(root, Color.ALICEBLUE);
TextField text = new TextField();
Label label = new Label();
VBox box = new VBox();
label.setText("This is a test label");
box.getChildren().add(label);
box.getChildren().add(text);
root.getChildren().add(box);
return (scene);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
initAndShowGUI();
}
});
}
}
The tests we are performing have very simple javafx with e.g. only an AnchorPane with a TextField on it and absolutely no code behind it, and just typing in the TextField is painfully slow.
Behavior looks very much like lock contention between the Swing and JavaFX threads, but it does not seem like we can find any explanation or solution.

This is not the answer that you are looking for, but since we have the same problem with Java 7 support, the answer is that Java 7 has reached its end of life:
July 2015: Updates for Java 7 are no longer available to the public.
Oracle offers updates to Java 7 only for customers who have purchased
Java support or have Oracle products that require Java 7.
https://www.java.com/en/download/faq/java_7.xml
There are no "good reasons" if problem solving is as easy as using a different java version. You don't break things by upgrading to Java 8.

Related

JavaFX ScrollBar setOnMousePressed not working

really liking JavaFX but have come across this problem and wondered if it was a bug.
The ScrollBar.setOnMousePressed() doesn't seem to fire when it has been initialised with a handler. The code below demonstrates the problem:-
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollBar;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Play extends Application {
public static void main(String[] args) {
launch(args);
}
private static int cnt;
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Bug?");
Button btn = new Button("This text will get replaced by the event handlers");
ScrollBar scrollBar = new ScrollBar();
// When pressing and releasing the ScrollBar thumb, we only get decrements
// If you replace the ScrollBar with say a Button, then the code below works as you might expect.
scrollBar.setOnMousePressed( event -> btn.setText("X" + cnt++));
scrollBar.setOnMouseReleased( event -> btn.setText("X" + cnt--));
VBox root = new VBox();
root.getChildren().add(btn);
root.getChildren().add(scrollBar);
primaryStage.setScene(new Scene(root, 350, 250));
primaryStage.show();
}
}
Note, Im running on JDK 1.8.0_66 64 Bit on Microsoft Windows 10.
A simple workaround, as suggested by James_D, is to use EventFilters instead of setOnMousePressed(), as follows:-
So,
scrollBar.addEventFilter(MouseEvent.MOUSE_PRESSED,
event -> btn.setText("X" + cnt++));
instead of
scrollBar.setOnMousePressed( event -> btn.setText("X" + cnt++));
I believe .setOnMousePressed() should work, but doesn't because of a bug in the library. I've raised with oracle and will update the answer once oracle clarifies.

Using Netbeans Visual Library in Javafx

I am trying to use the Net beans Visual Library in Javafx and I am referring to example here https://platform.netbeans.org/graph/examples.html.
I am particularly using the DemoGraphscene.java in the javaone.demo4 package. While I am using am using the example in my javafx project, I am not sure how to display the graph.
Here is what I have written in my controller class:
public class GraphicalViewController implements Initializable {
/**
* Initializes the controller class.
*/
#FXML
private Pane pane1;
#FXML
private Label label;
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
GraphScene scene = new DemoGraphScene ();
String helloNodeID = "Hello";
String worldNodeID = "World";
String edge = "edge";
Widget hello = scene.addNode (helloNodeID);
Widget world = scene.addNode (worldNodeID);
scene.addEdge (edge);
scene.setEdgeSource(edge, helloNodeID);
scene.setEdgeTarget(edge, worldNodeID);
hello.setPreferredLocation (new Point (100, 100));
world.setPreferredLocation (new Point (400, 200));
pane1.getChildren().add(scene.getView());
}
}
So I have (argument mismatch; JComponent cannot be converted to Node) in the line pane1.getChildren().add(scene.getView());
How do I come about this problem?
I did this:
SwingNode swingScene = new SwingNode();
SwingNode swingScene1 = new SwingNode();
swingScene.setContent(new JButton("Click me!"));
swingScene1.setContent(scene.getView());
pane1.getChildren().add(swingScene1);
When I do pane1.getChildren().add(swingScene1), I see nothing is displayed, but pane1.getChildren().add(swingScene) does show the button.
Use a SwingNode
The NetBeans Visual Library is Swing based. If you want to use it in JavaFX, you need to wrap the Swing component in a JavaFX node.
#FXML Pane pane1;
. . .
GraphScene scene = new DemoGraphScene();
. . .
SwingNode swingScene = new SwingNode();
swingScene.setContent(scene.getView());
. . .
pane1.getChildren().add(swingScene);
Notes of Confusion
It is especially confusing because the visual library works with a scene and the JavaFX library also works with a scene, but they are different scenes from different libraries, so you need to wrap the Visual Library scene view in a JavaFX node in order to display it in a JavaFX scene.
Additionally, when mixing Swing and JavaFX code, ensure you execute Swing code on the Swing event dispatch thread and JavaFX code on the JavaFX application thread - otherwise things could go badly wrong.
I'm not a big fan of mixing Swing and JavaFX, usually I recommend that if you want to use Swing, then write a 100% Swing App and if you want to use JavaFX, write a 100% JavaFX App.
Executable Sample
Here is an executable sample. The sample relies on the source code at: https://platform.netbeans.org/graph/examples.html. To use it, download the sample project from that link, paste the sample code below into the
NetBeans project and right-click to run it.
import java.awt.Dimension;
import java.awt.Point;
import javafx.application.Application;
import javafx.embed.swing.SwingNode;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import org.netbeans.api.visual.graph.GraphScene;
import org.netbeans.api.visual.widget.Widget;
public class FXGraphDemo extends Application {
#Override
public void start(final Stage stage) {
final SwingNode swingNode = new SwingNode();
createAndSetSwingContent(swingNode);
Scene scene = new Scene(new Group(swingNode), 400, 300);
stage.setScene(scene);
stage.show();
}
private void createAndSetSwingContent(final SwingNode swingNode) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
final GraphScene graphScene = new DemoGraphScene();
String helloNodeID = "Hello";
String worldNodeID = "World";
String edge = "edge";
Widget hello = graphScene.addNode (helloNodeID);
Widget world = graphScene.addNode (worldNodeID);
graphScene.addEdge (edge);
graphScene.setEdgeSource(edge, helloNodeID);
graphScene.setEdgeTarget(edge, worldNodeID);
hello.setPreferredLocation (new Point (100, 100));
world.setPreferredLocation (new Point (300, 200));
final JComponent sceneView = graphScene.createView();
final JScrollPane panel = new JScrollPane (sceneView);
panel.getHorizontalScrollBar().setUnitIncrement (32);
panel.getHorizontalScrollBar().setBlockIncrement (256);
panel.getVerticalScrollBar().setUnitIncrement (32);
panel.getVerticalScrollBar().setBlockIncrement (256);
panel.setPreferredSize(new Dimension (400, 300));
swingNode.setContent(panel);
}
});
}
public static void main(String[] args) {
launch(args);
}
}
Caveat
The sample (often, but not always) has a issue when it initially paints, it will flash black for a second and log a NullPointerExcepton when trying to calculate a Rectangle (for reasons unbeknownst to me - seems a bit like a race condition somewhere). After that though, it seems to display and work OK. To get around the black flash and NullPointerException, you can display the graph in a JFrame rather than a SwingNode, but then it shows in its own separate window rather than being embedded in a JavaFX scene.
NetBeans Visual Library JavaFX port
https://github.com/theanuradha/visual-library-fx/releases
https://github.com/theanuradha/visual-library-fx/tree/master/org-netbeans-api-visualfx/src/test/java/javaone/demo4
DemoGraphScene scene = new DemoGraphScene();
String helloNodeID = "Hello";
String worldNodeID = "World";
String edge = "edge";
Widget hello = scene.addNode(helloNodeID);
Widget world = scene.addNode(worldNodeID);
scene.addEdge(edge);
scene.setEdgeSource(edge, helloNodeID);
scene.setEdgeTarget(edge, worldNodeID);
hello.setPreferredLocation(new Point(0, 0));
world.setPreferredLocation(new Point(400, 200));
final SceneNode sceneView = scene.createView();

Code Layout regarding GUI Development

I'm currently learning to create Graphical Interfaces using JavaFX as i feel like it's
more powerful than Swing and easier to code by hand instead of having to resort to
GUIBuilder.
While this also includes Swing i have read quite a few tutorials so far and I always see
all the code being written in either the main() or start() methods.
For example this code example from java.about.com:
//Imports are listed in full to show what's being used
//could just import javafx.*
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class ApplicationWindow extends Application {
//JavaFX applicatoin still use the main method.
//It should only ever contain the call to the launch method
public static void main(String[] args) {
launch(args);
}
//starting point for the application
//this is where we put the code for the user interface
#Override
public void start(Stage primaryStage) {
//The primaryStage is the top-level container
primaryStage.setTitle("example Gui");
//The BorderPane has the same areas laid out as the
//BorderLayout layout manager
BorderPane componentLayout = new BorderPane();
componentLayout.setPadding(new Insets(20,0,20,20));
//The FlowPane is a conatiner that uses a flow layout
final FlowPane choicePane = new FlowPane();
choicePane.setHgap(100);
Label choiceLbl = new Label("Fruits");
//The choicebox is populated from an observableArrayList
ChoiceBox fruits = new ChoiceBox(FXCollections.observableArrayList("Asparagus", "Beans", "Broccoli", "Cabbage"
, "Carrot", "Celery", "Cucumber", "Leek", "Mushroom"
, "Pepper", "Radish", "Shallot", "Spinach", "Swede"
, "Turnip"));
//Add the label and choicebox to the flowpane
choicePane.getChildren().add(choiceLbl);
choicePane.getChildren().add(fruits);
//put the flowpane in the top area of the BorderPane
componentLayout.setTop(choicePane);
final FlowPane listPane = new FlowPane();
listPane.setHgap(100);
Label listLbl = new Label("Vegetables");
ListView vegetables = new ListView(FXCollections.observableArrayList("Apple", "Apricot", "Banana"
,"Cherry", "Date", "Kiwi", "Orange", "Pear", "Strawberry"));
listPane.getChildren().add(listLbl);
listPane.getChildren().add(vegetables);
listPane.setVisible(false);
componentLayout.setCenter(listPane);
//The button uses an inner class to handle the button click event
Button vegFruitBut = new Button("Fruit or Veg");
vegFruitBut.setOnAction(new EventHandler() {
#Override
public void handle(ActionEvent event) {
//switch the visibility for each FlowPane
choicePane.setVisible(!choicePane.isVisible());
listPane.setVisible(!listPane.isVisible());
}
});
componentLayout.setBottom(vegFruitBut);
//Add the BorderPane to the Scene
Scene appScene = new Scene(componentLayout,500,500);
//Add the Scene to the Stage
primaryStage.setScene(appScene);
primaryStage.show();
}
}
But coding everything in the main() or start() goes against principles i learnt at school
and also seems like a bad thing to do too me.
As i always used to code splitting everything up in different methods and than keeping a clean main() by only calling methods to do stuff, so it's easy to read up on the programs execution path. As i see all tutorials etc. going against this by putting al their GUI code in the main or start methods i started wondering if this is conventional to do regarding GUI code or should i still try to put everything under custom methods and keep a clean main()?
Also could anyone share neat tutorials regarding JavaFX? I can't seem to find much in this matter.
EDIT: I should really just ask for a good example on how a class hierarchy of an advanced application interface looks like to get some ideas.
Hope this was a clear question,
thanks in advance.
Jasper.
You could create a GUI class that holds all of your GUI code together, or simply create a GUI method that resides below main. Then, in main, you could call:
public static void main(String[] args)
{
launch(args);
}
public void start()
{
launchGUI(Stage primaryStage)
}
And then create your launchGUI method either in the same class, or create a new class specifically for the GUI and them use import to import it into the class containing main/start.

SWT Browser - Swing Integration - Mac - JDK 1.7

Right, so Ive got an interesting problem here concerning SWT and swing integration on mac running java 1.7. Im trying to embed an SWT Browser widget into my swing project as a panel which is pretty simple to do on java version 1.6. There has been a number of posts which explain how to do this with SWT_AWT bridge classes along with the following example:
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MySWTBrowserTest implements ActionListener {
public JButton addCodeButton;
public JButton launchBrowserButton;
public JTextField inputCode;
public JFrame frame;
static Display display;
static boolean exit;
public MySWTBrowserTest() {
frame = new JFrame("Main Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new FlowLayout());
inputCode = new JTextField(15);
inputCode.setText("999");
addCodeButton = new JButton("Add Code");
addCodeButton.addActionListener(this);
addCodeButton.setActionCommand("addcode");
launchBrowserButton = new JButton("Launch Browser");
launchBrowserButton.addActionListener(this);
launchBrowserButton.setActionCommand("launchbrowser");
mainPanel.add(inputCode);
mainPanel.add(addCodeButton);
mainPanel.add(launchBrowserButton);
frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("addcode")) {
} else if (e.getActionCommand().equals("launchbrowser")) {
createAndShowBrowser();
}
}
public void createAndShowBrowser() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Canvas canvas = new Canvas();
f.setSize(850, 650);
f.getContentPane().add(canvas);
f.setVisible(true);
display.asyncExec(new Runnable() {
#Override
public void run() {
Shell shell = SWT_AWT.new_Shell(display, canvas);
shell.setSize(800, 600);
Browser browser = new Browser(shell, SWT.NONE);
browser.setLayoutData(new GridData(GridData.FILL_BOTH));
browser.setSize(800, 600);
browser.setUrl("http://www.google.com");
shell.open();
}
});
}
public static void main(String args[]) {
//SWT_AWT.embeddedFrameClass = "sun.lwawt.macosx.CEmbeddedFrame";
display = new Display();
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
MySWTBrowserTest mySWTBrowserTest = new MySWTBrowserTest();
}
});
while (!exit) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Im using the swt-3.8M5-cocoa-macosx-x86_64 JAR files which obviously need to be included to run the above example. When using both the 32 bit and 64 bit versions of the 1.6 JDK, this runs perfectly fine, but when switching to the JDK 1.7 or 1.8 VM the reproducible error is thrown:
2012-05-14 15:11:30.534 java[1514:707] Cocoa AWT: Apple AWT Java VM was loaded on first thread -- can't start AWT. (
0 liblwawt.dylib 0x00000008db728ad0 JNI_OnLoad + 468
1 libjava.dylib 0x00000001015526f1 Java_java_lang_ClassLoader_00024NativeLibrary_load + 207
2 ??? 0x00000001015a4f90 0x0 + 4317663120
)
_NSJVMLoadLibrary: NSAddLibrary failed for /libjawt.dylib
JavaVM FATAL: lookup of function JAWT_GetAWT failed. Exit
Java Result: 255
Ive inspected the java 1.7 vm and did find the libraries there, so Im struggling to see what could cause it to not load that library. Of course I make sure to use: -XstartOnFirstThread as one of the VM parameters, as is required for the SWING/AWT integration.
On a further note, I have tried the DJ Native Widgets framework, and it throws the exact same error as it also uses the underlying SWT framework.
To reproduce the effects i suggest installing JDK 1.7 (release not the developer preview) on mac, downloading the: http://www.eclipse.org/downloads/download.php?file=/eclipse/downloads/drops4/S-4.2M7-201205031800/swt-S-4.2M7-201205031800-cocoa-macosx-x86_64.zip to get the library and then running it with the -XstartOnFirstThread -d64 java 1.7 vm.
really hoping someone has been able to sort this out as Im sure Im not the only one trying to integrate SWT into swing on the 1.7 vm
I also spent 8 hours on google to see if this error has been reproduced anywhere else, and it has come up on a few Matlab mailing lists, but other than that I haven't been able to find something even close to a solution.
Thanks in advance.
>> UPDATE 1
Looks like we may have a winner: https://bugs.eclipse.org/bugs/show_bug.cgi?id=374199
Going to monitor this and see where it goes.
>> UPDATE 2
Here is a working example: https://stackoverflow.com/a/27754819/363573
Unfortunately there's no good answer to this. In Java 7 the AWT has been completely rewritten to use CoreAnimation layers. The SWT assumes that an AWT Canvas will be backed by an NSView but that's no longer the case. Your only choice right now is to stick with Java 6.
The AWT team is aware of the problem but you may want to file another bug on bugs.sun.com.

Integrating JavaFX 2.0 WebView into a Swing Java SE 6 Application

I was looking for a way to integrate a Web-Browser-Component in an existing Swing-Application and found WebView for Java FX 2.0. Furthermore I found a blog post on java.net showing how to integrate a Java FX component into a Swing Application . So I guess it might be doable, but I haven't tried yet.
I'm curious, do you think this is a good approach? Are there any better solutions? Is it even doable? Is maybe something prebundled out there?
The motivation is: I want to integrate some WebBrowser-whatever into an existing Swing-Application, the long-term goal being to get rid of the whole Java Desktop Application at all, replacing it with a web-based solution (the plan is to slowly convert existing aspects into webpages which are then displayed in the WebBrowser-Component until nothing is left of the swing application except for the browser-skeleton). The backend of course remains Java :-)
I haven't tried yet since I simply lack the time to integrate JavaFX with my project (its a job, we're just exploring alternatives fpr the long run), so I better ask before I get burned.
It is very well possible!
One has to install JavaFX 2.0, and somehow manage to have jfxrt.jar in the Classpath.
The following code renders a JFXPanel inside a JFrame. The JFXPanel contains a WebView which loads google.com.
However, at least on my machine, the WebView feels rather sloppy. I'm working on Mac OS X 10.6; JavaFX 2.0 is still in beta for OS X.
Alternatives I found include MozSwing, which looked very promising and feels quite fast actually. Sadly the project is not being developed any further since 2008 and the bundled XUL runner is rather old (no new fancy html 5).
Both approaches are a nightmare to include via maven, you better setup your own local repository.
import java.awt.Dimension;
import java.awt.Point;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class JavaFX {
/* Create a JFrame with a JButton and a JFXPanel containing the WebView. */
private static void initAndShowGUI() {
// This method is invoked on Swing thread
JFrame frame = new JFrame("FX");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null); // do the layout manually
final JButton jButton = new JButton("Button");
final JFXPanel fxPanel = new JFXPanel();
frame.add(jButton);
frame.add(fxPanel);
frame.setVisible(true);
jButton.setSize(new Dimension(200, 27));
fxPanel.setSize(new Dimension(300, 300));
fxPanel.setLocation(new Point(0, 27));
frame.getContentPane().setPreferredSize(new Dimension(300, 327));
frame.pack();
frame.setResizable(false);
Platform.runLater(new Runnable() { // this will run initFX as JavaFX-Thread
#Override
public void run() {
initFX(fxPanel);
}
});
}
/* Creates a WebView and fires up google.com */
private static void initFX(final JFXPanel fxPanel) {
Group group = new Group();
Scene scene = new Scene(group);
fxPanel.setScene(scene);
WebView webView = new WebView();
group.getChildren().add(webView);
webView.setMinSize(300, 300);
webView.setMaxSize(300, 300);
// Obtain the webEngine to navigate
WebEngine webEngine = webView.getEngine();
webEngine.load("http://www.google.com/");
}
/* Start application */
public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
initAndShowGUI();
}
});
}
}

Categories