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

Learn how API management supports better integration in Achieving Enterprise Agility with Microservices and API Management, brought to you in partnership with 3scale

 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);
        }
      }
      
    });
   
  }

Unleash the power of your APIs with future-proof API management - Create your account and start your free trial today, brought to you in partnership with 3scale.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}