1 / 36

Swing 2 JavaFX

Swing 2 JavaFX. Steve Northover (Oracle) Anton Taraov (Oracle).

kimi
Télécharger la présentation

Swing 2 JavaFX

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Swing 2 JavaFX Steve Northover (Oracle) Anton Taraov (Oracle)

  2. The 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.

  3. Program Agenda • New and Exciting: SwingNode and Unified Threading! • Old and not so Exciting: Where we are Today • Discussion Topics: What should we do next?

  4. SwingNode • JFXPanel(JavaFX 2.0) • augments existing Swing based apps with FX features • the basis of an incremental migration strategy • SwingNode(JavaFX 8.0) • a reverse direction bridge to Swing • takes in legacy/commercial Swing components to modern FX apps

  5. API • As simple as: package javafx.embed.swing; public class SwingNode extends Node { public voidsetContent(final JComponentcontent); public JComponentgetContent(); } • Thread safe • JComponent : no hw components in the hierarchy, please

  6. Threading Restrictions • Invoke a UI related method on the right event thread: • Swing on Event Dispatchthread: SwingUtilities.invokeLater(Runnable r); • FX on JavaFX Application thread: Platform.runLater(Runnable r); • SwingNode belongs to FX! • Exclusion: SwingNode.setContent/getContent are thread safe methods

  7. Code Sample public class SwingInFx extends Application { @Override public void start(Stage stage) { final SwingNodeswingNode = new SwingNode(); createSwingContent(swingNode); Group group= new Group(); group.getChildren().add(swingNode); stage.setScene(new Scene(group, 100, 50)); stage.show(); } private void createSwingContent(final SwingNodeswingNode) { SwingUtilities.invokeLater(() -> swingNode.setContent(new JButton("Click me!"))); } }

  8. Approach • Symmetrical to JFXPanel: • lightweight embedding • events are forwarded from FX to Swing • SwingNode is a first class FX node: • transforms, effects, translucency

  9. Architecture

  10. Notifications • sun.swing.LightweightContent‑ implemented in SwingNode: • paint lock/unlock • image buffer update • focus grab/ungrab • preferred size update • cursor change

  11. Limitations • JViewPort: BLIT_SCROLL_MODE → BACKINGSTORE_SCROLL_MODE • Why? Bypassing paint() call, overridden by JLF's content pane • Looking for a better solution (getting somehow into RepaintManager) • Mac OS X: -Dswing.volatileImageBufferEnabled=false • Otherwise, performance drops significantly • Why? Bliting bits from VolatileImage to the shared memory

  12. Supported/planned functionality • Platforms: Windows/Mac/Linux • Key/Mouse/Wheel events • Popups auto-hiding • Custom Cursor change • Dirty Regions • Retina → JFX8 * • Focus traverse-out → JFX8 * • * Not a commitment. The targeted release number may be revised.

  13. Future improvements • Performance • DnD • Input methods • A11y • Modality / Z-order

  14. More Information: • “Embedding Swing Content in JavaFX Applications” • http://docs.oracle.com/javafx/8/embed_swing/jfxpub-embed_swing.htm • Relevant JavaOne talks (Swing and JavaFX): • Practical Pros and Cons of Replacing Swing with JavaFX in Existing Applications [CON3530] – Tuesday 11:30 • Moving from Swing to JavaFX [CON7852] – Thurs 12:30

  15. Unified Threading • Threading: the single biggest problem with Swing interop • Forces asynchronous programming on the desktop • Long considered to be “impossible to solve” • AWT/Swing threading is complex • Not dispatching events is equivalent to locking • “Two locks, two threads? You can deadlock …” General Rule of Thumb: Never wait, runLater()

  16. Threading: The Old Way JavaFX Operating System Event Queue AWT Swing Event Queue

  17. Threads in JavaFX and AWT/Swing • JavaFX is apartment threaded • The JavaFX platform GUI thread runs application code • If application code waits, it correctly blocks the UI • AWT is “free threaded” (not really …) • The Event Dispatch Thread (EDT) runs application code • The EDT is not the platform GUI thread • The AWT platform GUI thread cross posts events to EDT • Doesn’t wait (except for the next event from the operating system) • Application code doesn’t run in the platform GUI thread

  18. Threads in JavaFX and AWT/Swing • Swing is “apartment threaded”(not enforced) • The EDT runs application code (recall, it is not the GUI thread) • Waiting in the EDT correctly block the Swing UI “So both JavaFX and Swing are apartment threaded and neither one should wait while running application code. In fact, applications are often coded not to wait by using workers etc. Hmmm …”

  19. The Idea: 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”

  20. Unified Threading: The New Way JavaFX Operating System Event Queue AWT Swing Event Queue

  21. Unified Threading • Still under construction for JavaFX 8.0 • Early experiments are looking good • Threading is complex so there may be dragons • Enabled using a property • Djavafx.embed.singleThread=true • Disabled by default Try it out and let us know how you get on!

  22. To fill a shape with an image. Use existing picture box, DO NOT delete and create new picture box. Right click on the shape. At the bottom of the submenu select “Format Shape” Select “Fill” at the top of the “Format Shape” dialog box. Select “Picture or Texture fill” from the options. And select “File” under the “Insert from” option. Navigate to the file you want to use and select “Insert” On the “Format” tab, in the Size group, click on “Crop to Fill” in the Crop tool and drag the image bounding box to the desired size DELETE THIS INSTRUCTION NOTE WHEN NOT IN USE Old and not so Exciting: Where we are today

  23. JFXPanel: State of the Union • Code is stable and functionality is reasonable • Areas that need work: • Opacity, Exit on Close, • Performance, • Modality, DnD, • Layout, Popups auto-hiding, • Focus Traversal

  24. Opacity • [P3] RT-31361 Opacity is ignored for scene contained in a JFXPanel (Since b94) • [P3] JDK-8022512 JLightweightFrame: the content pane should be transparent • Note: use -Dswing.jlf.contentPaneTransparent=true to make the SwingNode’s background transparent

  25. Exit on Close • [P3] RT-29926 IllegalStateException using JFXPanel in a netbeans window. • [P4] RT-30536 JFXPanels are not reusable • WORK AROUND: Platform.setImplictExit(false) and call Platform.exit()

  26. Performance • [P3] RT-24278 JFXPanel with simple animation consumes entire CPU core (see PERF-326) • [P3] RT-22567 Minor tweaks to FX/Swing painting • Just need to get to these, we have lots of ideas

  27. Modalilty • [P4] RT-26080 FileChooser: cannot show modal when using jfxpanel.scene.window as owner window • WORK AROUND: Wrap JFXPanel in a JDialog. No work around for FileChooser

  28. DnD • [P3] RT-24337 Drag and drop window operations result in AssertionError with embedded JFXPanel components in NetBeans Platform Applications • [P2] RT-24273 Mac: Dragboard don't work correctly with JFXPane • [P3] RT-27014 Memory leak in EmbeddedScene.scenePaintListener or QuantumToolkit.dragSourceScene • [P3] RT-24890 DRAG_DONE event doesn't come

  29. Layout • [P4] RT-13858 JavaFX in Swing: no autosize on Propertychanges in Controls • [P4] RT-27053 JFXPanel: race condition when setting scene • Hoping to get to these for FX8

  30. Popups auto-hiding • [P4] RT-19953 PopupWindow.autoHide does not work in Swing application (includes ContextMenuetc) • Should be fixed in FX8. Work around in the JIRA

  31. Focus traversal • [P4] RT-10919 Provide possibility to traverse focus out of FX scene • This one is bad but we aren’t able to get to this for FX8

  32. To fill a shape with an image. Use existing picture box, DO NOT delete and create new picture box. Right click on the shape. At the bottom of the submenu select “Format Shape” Select “Fill” at the top of the “Format Shape” dialog box. Select “Picture or Texture fill” from the options. And select “File” under the “Insert from” option. Navigate to the file you want to use and select “Insert” On the “Format” tab, in the Size group, click on “Crop to Fill” in the Crop tool and drag the image bounding box to the desired size DELETE THIS INSTRUCTION NOTE WHEN NOT IN USE Discussion Topics: What should we do next?

  33. Discussion: What should we do next? • Areas that need work: • Retina support, • Accessibility, • Multi-touch, • More conversion methods (SwingFXUtils, Color, Font etc.) • … your input here …

More Related