integrating swing with javafx steve northover (client architect) kevin rushforth (consulting member...

32
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014 Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Upload: clementine-hunt

Post on 21-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Integrating Swing with JavaFX

Steve Northover (Client Architect)Kevin Rushforth (Consulting Member of Technical Staff)Java Client PlatformSept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Page 2: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Safe Harbor StatementThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

Page 3: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Program Agenda

Introduction

FX and Swing Working Together

The Threading Model

Implementation Details

Conclusion / Summary

1

2

3

4

5

Page 4: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

The Importance of Interop• Large number of existing AWT/Swing Applications• Don’t want or cannot rewrite legacy applications• Enable third-party AWT/Swing components• Allow people to “get their feet wet” with JavaFX• Provides a browser component for AWT/Swing

Page 5: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Goals• Provide a simple migration path for legacy applications• Provide full FX capability in the embedded application• Provide two way embedding for AWT/Swing• Ensure reasonable performance versus non-embedding

Page 6: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

History• Oct 2011: JavaFX 2.0 (JFXPanel – FX in AWT/Swing)• Nov 2011: JavaFX 2.0.2 (FXCanvas – FX in SWT)• Aug 2012: JavaFX 2.2 (Drag and drop, performance, threading)• Mar 2014: JavaFX 8.0 (SwingNode – AWT/Swing in FX)

Page 7: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

FX and Swing Working Together

Page 8: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Embedding Works Both Ways• JFXPanel is used to embed FX in AWT/Swing• SwingNode is used to embed AWT/Swing in JFX

What happens when a SwingNode is embeddedin a JFXPanel in a SwingNode in a JFXPanel …?

Page 9: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

JFXPanel: A Swing Component• JFXPanel is a lightweight JComponent• Multiple JFXPanels supported in a single application• Overrides event handlers and forwards to JavaFX• Performance is comparable to stand alone JavaFX• Supports accelerated painting, drag and drop, etc.

Page 10: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Oracle Confidential – Internal/Restricted/Highly Restricted 10

JFXPanel: A Complete Examplepublic static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("AWT/Swing"); frame.setBounds(100, 100, 200, 225); JFXPanel fxPanel = new JFXPanel(); frame.add(fxPanel); frame.setVisible(true); Platform.runLater(() -> { Group group = new Group(new Circle(100, 100, 100, Color.RED)); Scene scene = new Scene(group, 200, 200); fxPanel.setScene(scene); }); });}

Page 11: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

JFXPanel: SwingBrowser Example

Page 12: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

SwingNode: A JavaFX Node• SwingNode is a regular JavaFX node• Multiple SwingNode supported in a single application• Overrides event handlers and forwards to AWT/Swing• Performance is comparable to stand alone AWT/Swing (*)• Supports accelerated painting, drag and drop, etc. (*)

(*) Many problems in these areas have been fixed in 8u40

Page 13: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. Oracle Confidential – Internal/Restricted/Highly Restricted 13

SwingNode: A Complete Example public void start(Stage stage) {

SwingNode swingNode = new SwingNode();

SwingUtilities.invokeLater(() -> {

swingNode.setContent(new JButton("Click me!"));

});

StackPane pane = new StackPane();

pane.getChildren().add(swingNode);

stage.setScene(new Scene(pane, 100, 50));

stage.show();

}

Page 14: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

SwingNode: SwingSet and JFreeChart Example

Page 15: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Current Limitations• Must turn off implicit exit (RT-29926, RT-30536)

– WORK AROUND: Use Platform.setImplictExit(false)

• Cannot traverse out of an embedding (RT-10919, RT-31462)• Cannot show modal window as owner window (RT-26080)

– WORK AROUND: Wrap JFXPanel in a JDialog

• SwingNode performance / painting (RT-37046, RT-37810 …)

Page 16: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

The Threading Model

Page 17: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

The Interop Threading Model• AWT/Swing has its own threading model and event loop

– AWT is “free threaded” (not really, it doesn’t work well)– Swing is apartment threaded (a single UI-thread, called EDT)– The event loop is native and runs in another thread (not the EDT)

• JavaFX also has its own threading model and event loop– JavaFX is apartment threaded (a single UI-thread)– The event loop is native and runs in the single UI-thead

Page 18: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

The Interop Threading Model

Operating System

Event Queue

JavaFX

Event Queue

AWTSwing

runLater()

invokeLater()

Page 19: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Problems with the Current Model • Must never use invokeAndWait() or risk deadlock• Forces asynchronous programming (similar to the web)• Applications become more complex for no good reason• Threading is not consistently enforced

– JavaFX contains thread checks in many places but not everywhere– Accessing data from the wrong thread can cause intermittent errors

Page 20: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Towards a Unified Threading Model• Both JavaFX and Swing are apartment threaded• Both toolkits run in separate apartments• Neither toolkit should wait when running application code

– If JavaFX waits, GUI input is (correctly) blocked– Swing code should never wait in an event handler

• The JavaFX apartment is the same as the native GUI-thread– This cannot be changed, FX must run in this thread

Could Swing code run in the FX GUI-thread?

Page 21: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Unified Threading: Marry the Threads

“Why not make the JavaFX platform GUI thread be the EDT thread for AWT/Swing? It is required that the EDT be non-GUI for AWT/Swing, but there is nothing that says it can’t be a GUI thread for someone else.”

Page 22: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

The Unified Threading Model

Operating System

Event Queue

JavaFX

Event Queue

AWTSwing

Page 23: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Unified Threading: It works but …• The code has been released in JDK8• Turned off by default

– Use -Djavafx.embed.singleThread=true

• The approach requires more testing– Threading is complex so there be dragons

Try it out and let us know how you get on!

Page 24: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Implementation Details

Page 25: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Two Possible Approaches• Heavyweight

– “easy” to implement, harder to use– Limited set of interoperability features (*)

• No translucency, no effects, no transformation, etc.

• Lightweight– “harder” to implement, easier to use– Provides better integration with the embedding toolkit

• Full translucency, full effects, optimized scrolling etc.

(*) Suffers from the same problems as mixing lightweight/heavyweight in AWT/Swing

Page 26: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Lightweight Implementations• Many different advantages over heavyweight• Really, the only viable alternative to remain full featured• One serious disadvantage

– Embedding toolkit must implement all new low level features– Glass native code cannot be used

• Example:– JavaFX Drag and drop must be implemented using AWT/Swing drag and drop– JavaFX Touch must (but cannot) be implemented using AWT/Swing

Page 27: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

JFXPanel: A Lightweight Implementation

Swing JavaFX

Swing input events convertedto JavaFX input events

JavaFX textures convertedto image data for paintingby Swing

Page 28: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

SwingNode: A Lightweight Implementation

Swing JavaFX

JavaFX input events convertedto Swing input events

Swing images convertedto textures for paintingby JavaFX

Page 29: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Conclusions / Summary

Page 30: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Conclusions / Summary• AWT/Swing and JavaFX interoperate (almost) freely• The Unified Thread Model holds great promise• Both Embedding directions are supported• JFXPanel is mature and stable• SwingNode is newer with a few problem areas• AWT/Swing interop, although not preferred, is a viable

way to build JavaFX applications

Page 31: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Safe Harbor StatementThe preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

Page 32: Integrating Swing with JavaFX Steve Northover (Client Architect) Kevin Rushforth (Consulting Member of Technical Staff) Java Client Platform Sept, 2014