Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Resolve Modality Issues in Hybrid Desktop Apps (Swing and JavaFX)

DZone's Guide to

Resolve Modality Issues in Hybrid Desktop Apps (Swing and JavaFX)

· Integration Zone ·
Free Resource

SnapLogic is the leading self-service enterprise-grade integration platform. Download the 2018 GartnerMagic Quadrant for Enterprise iPaaS or play around on the platform, risk free, for 30 days.

 It is still not recommend to mix  the outdated Swing GUI toolkit and JavaFX but still there are hell of a lot apps that use Swing and need to be skinned in order to look a little bit better. In the hybrid apps it's important that JavaFX  scenes and containers are created and updated on JavaFX application thread by invoking:

Platform.runLater(new Runnable(){
@Override
public void run(){
//create Scene
}
});

Please note that long-running computations on JavaFX Thread may cause unresponsive UI and therefore it's highly recommended to use the implementation of FutureTask from javafx.concurrent package Task<V> and Service<V> that implements Worker interface to perform some work on one or more background threads.  

Swing Components should be update on EDT:

SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
//update Swing JComponents
}

});

JavaFX SDK offers JFXPanel that enables to embed JavaFX scenes within Swing components.

Modality is the major problem among others in this hybrid approach and the users are driven to madness since they are so used to modal JDialogs (Swing UI components in the background can not be used untill the user does not close the modal dialog ). The problem really occurs when devs will face a situation where a JFrame is used as parent components and embeds JFXPanels.

The following code snipped will make your life slightly easier  regarding modality issues untill the managment desides to migrate completly to JavaFX as primary front-end toolkit:

public static void activateComponent(final Component targetComponent, final boolean isActive){
    SwingUtilities.invokeLater(new Runnable(){

      @Override
      public void run() {
        Window window = SwingUtilities.windowForComponent(targetComponent);
        if (window != null) {
          window.setEnabled(isActive);
        }
      }
      
    });
   
  }

With SnapLogic’s integration platform you can save millions of dollars, increase integrator productivity by 5X, and reduce integration time to value by 90%. Sign up for our risk-free 30-day trial!

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}