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

Make javafx.scene.text.Text non-final

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • fx2.1
    • None
    • javafx

      A Java development pattern that my team has found very useful is the use of double brace initialization. We have been using this in our production scene graph projects using Piccolo2D and Swing, and would like to be able to apply it in JavaFX as well as we evaluate it for use in our projects. For instance, the idiomatic way to create a Text instance and add it to a scene group could be something like:

      Text text = new Text( 20, 110, "JavaFX" );
      text.setFill( Color.DODGERBLUE );
      text.setEffect( new Lighting() );
      text.setFont( Font.font( "Amble Bold", 50 ) );
      rootGroup.getChildren().add( text );

      We would prefer to use double brace initialization, to have the code read like this:

      getChildren().add( new Text( 20, 110, "JavaFX" ) {{
      ....setFill( Color.DODGERBLUE );
      ....setEffect( new Lighting() );
      ....setFont( Font.font( "Amble Bold", 50 ) );
      }} );

      This provides several advantages:
      1. it gives more a feel of structural declaration (like in JavaFX Script).
      2. it is easier to write and read/understand, and scales up nicely
      3. instead of creating and mutating the text instance, it is already correct upon instantiation, thus preventing mutation-based bugs
      4. it is more difficult to create the bug in which the node is created but not added to the scene.
      5. it is more difficult to create copy/paste bugs when the above block of code is duplicated to make and add a similar instance.

      However, JavaFX's class Text is final, and therefore prevents subclassing in this manner. Can you explain why class Text is final? (None of the other Shape subclasses are final.) Can it be changed to be non-final before you come out of beta? Our workaround would be to create a non-final wrapper class for Text, but it would be better to avoid this creation/maintenance cost if possible.

      Thanks!
      Sam

            prr Philip Race
            duke J. Duke
            Votes:
            2 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: