Presentation is loading. Please wait.

Presentation is loading. Please wait.

Integrating Swing with JavaFX

Similar presentations


Presentation on theme: "Integrating Swing with JavaFX"— Presentation transcript:

1 Integrating Swing with JavaFX
This is a Title Slide with Java FY15 Theme slide ideal for including the Java Theme with a brief title, subtitle and presenter information. To customize this slide with your own picture: Right-click the slide area and choose Format Background from the pop-up menu. From the Fill menu, click Picture and texture fill. Under Insert from: click File. Locate your new picture and click Insert. To copy the Customized Background from Another Presentation on PC Click New Slide from the Home tab's Slides group and select Reuse Slides. Click Browse in the Reuse Slides panel and select Browse Files. Double-click the PowerPoint presentation that contains the background you wish to copy. Check Keep Source Formatting and click the slide that contains the background you want. Click the left-hand slide preview to which you wish to apply the new master layout. Apply New Layout (Important): Right-click any selected slide, point to Layout, and click the slide containing the desired layout from the layout gallery. Delete any unwanted slides or duplicates. To copy the Customized Background from Another Presentation on Mac Click New Slide from the Home tab's Slides group and select Insert Slides from Other Presentation… Navigate to the PowerPoint presentation file that contains the background you wish to copy. Double-click or press Insert. This prompts the Slide Finder dialogue box. Make sure Keep design of original slides is unchecked and click the slide(s) that contains the background you want. Hold Shift key to select multiple slides. Apply New Layout (Important): Click Layout from the Home tab's Slides group, and click the slide containing the desired layout from the layout gallery. 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.

2 This is a Safe Harbor Front slide, one of two Safe Harbor Statement slides included in this template. One of the Safe Harbor slides must be used if your presentation covers material affected by Oracle’s Revenue Recognition Policy To learn more about this policy, For internal communication, Safe Harbor Statements are not required. However, there is an applicable disclaimer (Exhibit E) that should be used, found in the Oracle Revenue Recognition Policy for Future Product Communications. Copy and paste this link into a web browser, to find out more information. For all external communications such as press release, roadmaps, PowerPoint presentations, Safe Harbor Statements are required. You can refer to the link mentioned above to find out additional information/disclaimers required depending on your audience.

3 Program Agenda 1 Introduction FX and Swing Working Together The Threading Model Implementation Details Conclusion / Summary 2 3 4 5

4 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

5 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

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

7 FX and Swing Working Together

8 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 embedded in a JFXPanel in a SwingNode in a JFXPanel …?

9 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.

10 JFXPanel: A Complete Example
public 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); }); } Oracle Confidential – Internal/Restricted/Highly Restricted

11 JFXPanel: SwingBrowser Example

12 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

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(); } Oracle Confidential – Internal/Restricted/Highly Restricted

14 SwingNode: SwingSet and JFreeChart Example

15 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 …)

16 The Threading Model

17 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

18 The Interop Threading Model
Operating System Event Queue JavaFX AWT Swing runLater() invokeLater()

19 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

20 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?

21 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.”

22 The Unified Threading Model
Operating System Event Queue JavaFX AWT Swing

23 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!

24 Implementation Details

25 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

26 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

27 JFXPanel: A Lightweight Implementation
Swing JavaFX Swing input events converted to JavaFX input events JavaFX textures converted to image data for painting by Swing

28 SwingNode: A Lightweight Implementation
JavaFX JavaFX input events converted to Swing input events Swing images converted to textures for painting by JavaFX

29 Conclusions / Summary

30 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

31 This is a Safe Harbor Front slide, one of two Safe Harbor Statement slides included in this template. One of the Safe Harbor slides must be used if your presentation covers material affected by Oracle’s Revenue Recognition Policy To learn more about this policy, For internal communication, Safe Harbor Statements are not required. However, there is an applicable disclaimer (Exhibit E) that should be used, found in the Oracle Revenue Recognition Policy for Future Product Communications. Copy and paste this link into a web browser, to find out more information. For all external communications such as press release, roadmaps, PowerPoint presentations, Safe Harbor Statements are required. You can refer to the link mentioned above to find out additional information/disclaimers required depending on your audience.

32


Download ppt "Integrating Swing with JavaFX"

Similar presentations


Ads by Google