Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8290844

Add Skin.install() method



    • Enhancement
    • Resolution: Fixed
    • P4
    • jfx20
    • None
    • javafx
    • None
    • b06



      Introduce a new Skin.install() method with an empty default implementation. Modify Control.setSkin(Skin) implementation to invoke install() on the new skin after the old skin has been removed with dispose().


      Presently, switching skins is a two-step process: first, a new skin is constructed against the target Control instance, and is attached (i.s. listeners added, child nodes added) to that instance in the constructor. Then, Control.setSkin() is invoked with a new skin - and inside, the old skin is detached via its dispose() method.

      This creates two problems:

       1. if the new skin instance is discarded before setSkin(), it remains attached, leaving the control in a weird state with two skins attached, causing memory leaks and performance degradation.

       2. if, in addition to adding listeners and child nodes, the skin sets a property, such as an event listener, or a handler, it overwrites the current value irreversibly. As a result, either the old skin would not be able to cleanly remove itself, or the new skin would not be able to set the new values, as it does not know whether it should overwrite or keep a handler installed earlier (possibly by design). Unsurprisingly, this also might cause memory leaks.

      A number of related bugs have been collected under JDK-8241364 ☂ Cleanup skin implementations to allow switching, which refers a number of bugs:

      JDK-8245145 Spinner: throws IllegalArgumentException when replacing skin
      JDK-8245303 InputMap: memory leak due to incomplete cleanup on remove mapping
      JDK-8268877 TextInputControlSkin: incorrect inputMethod event handler after switching skin


      This problem does not exist in e.g. Swing because the steps of instantiation, uninstalling the old ComponentUI ("skin"), and installing a new skin are cleanly separated. ComponentUI constructor does not alter the component itself, ComponentUI.uninstallUI(JComponent) cleanly removes the old skin, ComponentUI.installUI(JComponent) installs the new skin. We should follow the same model in javafx.

      Specifically, I'd like to propose the following changes:

       1. Add Skin.install() with a default no-op implementation.
       2. Clarify skin life cycle (creation-attachment-detachment sequence) in Skin and Skin.install() javadoc
       3. Modify Control.setSkin(Skin) method (== invalidate listener in skin property) to call oldSkin.dispose() followed by newSkin.install()
       4. Check whether (newSkin.getSkinnable() == this) inside of Control.setSkin(Skin) property handler, and throw an Error if it isn't so (when newSkin is != null of course).
       5. Many existing skins that do not set properties in the corresponding control may remain unchanged. The skins that do, such as TextInputControlSkin (JDK-8268877), must be refactored to utilize the new install() method. I think the refactoring would simply move all the code that accesses its control instance away from the constructor to install().

      Impact Analysis

      see corresponding CSR JDK-8292213

      see corresponding CSR JDK-8292213


        Issue Links



              angorya Andy Goryachev
              angorya Andy Goryachev
              0 Vote for this issue
              4 Start watching this issue