{"id":1843,"date":"2015-01-28T17:04:10","date_gmt":"2015-01-28T16:04:10","guid":{"rendered":"http:\/\/www.jensd.de\/wordpress\/?p=1843"},"modified":"2015-04-05T14:46:36","modified_gmt":"2015-04-05T12:46:36","slug":"jfxpanel-and-fx-platform-thread-pitfalls","status":"publish","type":"post","link":"https:\/\/www.jensd.de\/wordpress\/?p=1843","title":{"rendered":"JFXPanel and FX Platform Thread pitfalls"},"content":{"rendered":"<p>The JFXPanel is a component to embed JavaFX content into (legacy ;-)) Swing applications.<br \/>\nBasically it makes it very easy to combine both tookits, but there are some pitfalls to master:<br \/>\nBoth UI Toolkits are single threaded (Swing: EDT + JavaFX: FX Platform Thread). When used together you have to deal with these two threads, e.g.<br \/>\n<code><strong>javafx.embed.swing.SwingFXUtils.runOnFxThread(Runnable runnable)<\/strong><\/code><br \/>\nor<br \/>\n<code><strong>javafx.embed.swing.SwingFXUtils.runOnEDT(Runnable runnable) <\/strong><\/code><\/p>\n<p>The FX Platform Thread is implicitly started in the constructor of the JFXPanel by initFx():<\/p>\n<p>[java]<br \/>\n\/\/ Initialize FX runtime when the JFXPanel instance is constructed<br \/>\nprivate synchronized static void initFx() {<br \/>\n    \/\/ Note that calling PlatformImpl.startup more than once is OK<br \/>\n    PlatformImpl.startup(new Runnable() {<br \/>\n        @Override public void run() {<br \/>\n            \/\/ No need to do anything here<br \/>\n        }<br \/>\n    });<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>But (if I got it right) JFXPanel overrides addNotify() from Component where a finishListener is added to the FX-Platform ( <code><strong>PlatformImpl.addListener(finishListener)<\/strong><\/code>). <code><strong>Platform.exit<\/strong><\/code> is then called when the last JFXPanel &#8220;dies&#8221;.<\/p>\n<p><strong>This might lead into a weird situation:<\/strong><\/p>\n<p>When JFXPanel is used e.g. with a JDialog: The first call opens the Dialog with a new JFXPanel and all is going well.<br \/>\nBut when this Dialog is closed the FX Platform Thread is exited and for some reasons it looks like the second call to open a new Dialog doesn&#8217;t start the FX Platform Thread again. So nothing happens on the JFXPanel!<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>For me it worked to call (somewhere early in main())<br \/>\n<code><strong>Platform.setImplicitExit(false);<\/strong><\/code><br \/>\nto prevent closing the FX Thread implicitly (its closed then by the System.exit()).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The JFXPanel is a component to embed JavaFX content into (legacy ;-)) Swing applications. Basically it makes it very easy to combine both tookits, but there are some pitfalls to master: Both UI Toolkits are single threaded (Swing: EDT + JavaFX: FX Platform Thread). When used together you have to deal with these two threads,&hellip; <span class=\"clear\"><\/span><a href=\"https:\/\/www.jensd.de\/wordpress\/?p=1843\" class=\"more-link read-more\" rel=\"bookmark\">Continue Reading <span class=\"screen-reader-text\">JFXPanel and FX Platform Thread pitfalls<\/span><i class=\"fa fa-arrow-right\"><\/i><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"footnotes":"","_jetpack_memberships_contains_paid_content":false,"jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[4,40],"tags":[76,99],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p38FCL-tJ","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1843"}],"collection":[{"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1843"}],"version-history":[{"count":12,"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1843\/revisions"}],"predecessor-version":[{"id":2024,"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1843\/revisions\/2024"}],"wp:attachment":[{"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jensd.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}