Class VFXCellBase<T>

java.lang.Object
javafx.scene.Node
javafx.scene.Parent
javafx.scene.layout.Region
javafx.scene.control.Control
io.github.palexdev.mfxcore.controls.Control<CellBaseBehavior<T>>
io.github.palexdev.virtualizedfx.cells.VFXCellBase<T>
All Implemented Interfaces:
io.github.palexdev.mfxcore.behavior.WithBehavior<CellBaseBehavior<T>>, io.github.palexdev.mfxcore.controls.MFXSkinnable<io.github.palexdev.mfxcore.controls.SkinBase<?,?>>, io.github.palexdev.mfxcore.controls.MFXStyleable, VFXCell<T>, Styleable, EventTarget, Skinnable
Direct Known Subclasses:
VFXSimpleCell, VFXSimpleTableCell

public abstract class VFXCellBase<T> extends io.github.palexdev.mfxcore.controls.Control<CellBaseBehavior<T>> implements VFXCell<T>, io.github.palexdev.mfxcore.controls.MFXStyleable
The basic and typical implementation of a cell in JavaFX is a cell with just two properties: one to keep track of the cell's index and the other to keep track of the displayed item.

This abstract class is a good starting point to implement concrete, usable cells.

Unusually, this enforces the structuring of cells as specified by the MVC pattern. In fact, this extends Control, expects behaviors of type CellBaseBehavior and doesn't come with a default skin.

The idea is to make the skin implementation responsible for how data is represented (a String, a Node, processing, etc.). A downside of such approach is that for some reason, users are a bit reluctant in making or customizing skins, however, I can assure you it's no big deal at all. Also, never forget that VirtualizedFX containers do not enforce the usage of VFXCellBase or any of its implementations, if you are more comfortable using a simpler cell system you are free do it.

The default style class is 'cell-base'.

A word on performance As also stated in the javadocs of updateIndex(int) and updateItem(Object), to simplify the internal management of cells, there is no 100% guarantee that updates are called with a different index/item from what the cell currently has. For this reason, here's how you should implement the 'rendering' operations:

You probably want to execute some operations when the index or item change by listening to the respective properties, indexProperty() and itemProperty(). This means that your operations will run only and only if the property fires an invalidation/change event. In this base class the updateIndex(int) and updateItem(Object) methods are implemented naively, because we work on generic items, we don't know the model, which means that they update the respective properties without any check.

- For the indexProperty() it's tricky: the JVM caches Integers between -128 and 127, which means that the '==' operator only works in that range; for larger datasets, you may want to override the updateIndex(int) method to actually check for equality.

For the itemProperty() it's the same concept. If you know the model, you may want to perform some equality check in the updateItem(Object) method to avoid useless updates. For example, if in your dataset there are two Person objects with the same attributes but different references you may want to update the property (so that the reference is correct) but not perform any operation that strictly depends on the attributes (if a label displays the attributes, there's no need to re-compute the text)

See Also:
  • Property Details

  • Constructor Details

    • VFXCellBase

      public VFXCellBase(T item)
  • Method Details

    • defaultStyleClasses

      public List<String> defaultStyleClasses()
      Specified by:
      defaultStyleClasses in interface io.github.palexdev.mfxcore.controls.MFXStyleable
    • defaultBehaviorProvider

      public Supplier<CellBaseBehavior<T>> defaultBehaviorProvider()
      Specified by:
      defaultBehaviorProvider in interface io.github.palexdev.mfxcore.behavior.WithBehavior<T>
    • toNode

      public Node toNode()
      Description copied from interface: VFXCell
      Converts the cell to a Node.

      Implementations can check these examples:

       
       // Example 1
       public class SimpleCell<T> extends Label implements VFXCell<T> {
           ...
           ...
      
           @Override
           public Node toNode() {
               return this;
           }
       }
      
       // Example 2
       public class SimpleCell<T> implements VFXCell<T> {
           private final Label label = ...;
           ...
           ...
      
           @Override
           public Node toNode() {
               return label;
           }
       }
       
       
      Specified by:
      toNode in interface VFXCell<T>
    • updateItem

      public void updateItem(T item)
      Automatically called by the framework when the cell needs to update its item.

      Note though, that there is no 100% guarantee the new given item is different from the current one. If you have some operations happen when the item changes, for performance reasons, I recommend you to first ensure the new item really is different.

      See VFXCellBase and read how this is handled.

      Updates the itemProperty().
      Specified by:
      updateItem in interface VFXCell<T>
    • updateIndex

      public void updateIndex(int index)
      Automatically called by the framework when the cell needs to update its index.

      Note though, that there is no 100% guarantee the new given index is different from the current one. If you have some operations happen when the index changes, for performance reasons, I recommend you to first ensure the new index really is different.

      See VFXCellBase and read how this is handled.

      Updates the indexProperty().
      Specified by:
      updateIndex in interface VFXCell<T>
    • onCreated

      public void onCreated(VFXContext<T> context)
      Called when a cell is created and associated with a VFXContainer. This method provides the cell with a reference to its container, along with additional services that the cell may use to extend its functionalities.

      It's up to the implementation to decide how to use it.

      Note: This method should only be called by the container that created this cell. Calling it with a different or incorrect container instance may lead to inconsistent behavior or errors.

      The implementation stores the context and prevents overwrites once the instance is set (not null anymore).
      Specified by:
      onCreated in interface VFXCell<T>
      See Also:
    • dispose

      public void dispose()
      Description copied from interface: VFXCell
      Automatically called by the framework when the cell is not needed anymore. The user can override this to perform some operations before the cell is GCd.
      Specified by:
      dispose in interface VFXCell<T>
    • getAlignment

      public Pos getAlignment()
      Gets the value of the alignment property.
      Property description:
      Specifies the alignment of the displayed data. How this is used depends on the skin implementation.

      This is settable via CSS with the "-fx-alignment" property.

      Returns:
      the value of the alignment property
      See Also:
    • alignmentProperty

      public io.github.palexdev.mfxcore.base.properties.styleable.StyleableObjectProperty<Pos> alignmentProperty()
      Specifies the alignment of the displayed data. How this is used depends on the skin implementation.

      This is settable via CSS with the "-fx-alignment" property.

      Returns:
      the alignment property
      See Also:
    • setAlignment

      public void setAlignment(Pos alignment)
      Sets the value of the alignment property.
      Property description:
      Specifies the alignment of the displayed data. How this is used depends on the skin implementation.

      This is settable via CSS with the "-fx-alignment" property.

      Parameters:
      alignment - the value for the alignment property
      See Also:
    • getClassCssMetaData

      public static List<CssMetaData<? extends Styleable, ?>> getClassCssMetaData()
    • getControlCssMetaData

      protected List<CssMetaData<? extends Styleable, ?>> getControlCssMetaData()
      Overrides:
      getControlCssMetaData in class Control
    • context

      protected VFXContext<T> context()
      See Also:
    • getIndex

      public int getIndex()
      Gets the value of the index property.
      Property description:
      Specifies the cell's index.
      Returns:
      the value of the index property
      See Also:
    • indexProperty

      public IntegerProperty indexProperty()
      Specifies the cell's index.
      Returns:
      the index property
      See Also:
    • setIndex

      public void setIndex(int index)
      Sets the value of the index property.
      Property description:
      Specifies the cell's index.
      Parameters:
      index - the value for the index property
      See Also:
    • getItem

      public T getItem()
      Gets the value of the item property.
      Property description:
      Specifies the cell's item.
      Returns:
      the value of the item property
      See Also:
    • itemProperty

      public ObjectProperty<T> itemProperty()
      Specifies the cell's item.
      Returns:
      the item property
      See Also:
    • setItem

      public void setItem(T item)
      Sets the value of the item property.
      Property description:
      Specifies the cell's item.
      Parameters:
      item - the value for the item property
      See Also:
    • getGraphic

      public Node getGraphic()
      Gets the value of the graphic property.
      Property description:
      Allows adding a Node to the cell. To be precise, how this property is used depends on the skin implementation.
      Returns:
      the value of the graphic property
      See Also:
    • graphicProperty

      public ObjectProperty<Node> graphicProperty()
      Allows adding a Node to the cell. To be precise, how this property is used depends on the skin implementation.
      Returns:
      the graphic property
      See Also:
    • setGraphic

      public void setGraphic(Node graphic)
      Sets the value of the graphic property.
      Property description:
      Allows adding a Node to the cell. To be precise, how this property is used depends on the skin implementation.
      Parameters:
      graphic - the value for the graphic property
      See Also: