2 Replies Latest reply: Jul 3, 2013 6:42 AM by fabsav RSS

    Custom Components skin

    fabsav

      Hi!

      do someone knows the right way to implement a class that implements Skin?

       

      I tried this kind of code and it seem to work, but I'm uncertain about methods implementation.

       

      public class XSkin implements Skin<X> {
             private X xInstance;
      
             XSkin(X x){
                  this.xInstance = x;
             }
      
             @Override
             public void dispose() {
                  xInstance = null;
             }
      
             @Override
             public Node getNode() {
                  layout.getStyleClass().add("css-class");
                  return layout;
             }
      
      
             @Override
             public X getSkinnable() {
                  return xInstance;
             }
        }
      

       

      Any tips for me?

       

      Thank you

        • 1. Re: Custom Components skin
          csh

          I always do it like this:

           

          public final class XSkin extends StackPane implements Skin<X> {
          
              private final X control;
          
              public XSkin(final X control) {
                  this.control = control;
              }
          
              @Override
              public X getSkinnable() {
                  return control;
              }
          
              @Override
              public Node getNode() {
                  return this;
              }
          
              @Override
              public void dispose() {
                  // unbind properties and remove listeners
              }
          }
          

           

          Instead of StackPane you also could use any other Node, depending on what you need. But SkinBase also uses StackPane.

           

          I don't why one should set the control to null in the dispose method. Actually this is not needed I think.

           

          More important is to remove any listeners you set up in your skin. (e.g. if you have a TextField in your skin and you add a listener to the textProperty()).

           

          If you use a binding between the control and the skin, e.g. skin.myProperty.bind(control.myProperty()) you should unbind skin.myProperty. Otherwise if the skin changes, the control has still a reference to the old skin, due to this bug: https://javafx-jira.kenai.com/browse/RT-31031

          • 2. Re: Custom Components skin
            fabsav

            Your way sounds good

            I'll try to improve my solution. And now i know what to do in dispose method

             

            Thank you!