5.5.9.1. Working with Swing Components

In order to work directly with Swing components that implement interfaces of the visual components library in Desktop Client, use the following methods of the Component interface:

  • unwrap() – retrieves an underlying Swing component for a given CUBA component.

  • unwrapComposition() - retrieves a Swing component that is the outmost external container in the implementation of a given CUBA component. For simple components, such as Button, this method returns the same object as unwrap() - javax.swing.JButton. For complex components, such as Table, unwrap() will return the corresponding org.jdesktop.swingx.JXTable instance, while unwrapComposition() will return an instance of javax.swing.JPanel, which contains a table together with ButtonsPanel and RowsCount defined with it.

The methods accept a class of the underlying component to be returned, for example:

javax.swing.JButton jButton = button.unwrap(javax.swing.JButton.class);

You can also use the unwrap() and getComposition() static methods of the DesktopComponentsHelper class, passing a CUBA component into them.

Please note that if a screen is located in the project’s gui module, you can only work with generalized interfaces of CUBA components. In order to use unwrap() you should either put the entire screen in the desktop module, or use the controller companions mechanism.