Class GridView<T>

  • All Implemented Interfaces:
    Styleable, EventTarget, Skinnable

    public class GridView<T>
    extends Control
    A GridView is a virtualised control for displaying getItems() in a visual, scrollable, grid-like fashion. In other words, whereas a ListView shows one ListCell per row, in a GridView there will be zero or more GridCell instances on a single row.

    This approach means that the number of GridCell instances instantiated will be a significantly smaller number than the number of items in the GridView items list, as only enough GridCells are created for the visible area of the GridView. This helps to improve performance and reduce memory consumption.

    Because each GridCell extends from Cell, the same approach of cell factories that is taken in other UI controls is also taken in GridView. This has two main benefits:

    1. GridCells are created on demand and without user involvement,
    2. GridCells can be arbitrarily complex. A simple GridCell may just have its text property set, whereas a more complex GridCell can have an arbitrarily complex scenegraph set inside its graphic property (as it accepts any Node).


    The following screenshot shows the GridView with the ColorGridCell being used:
    Screenshot of GridView

    To create this GridView was simple. Note that the majority of the code below is related to randomly creating the colours to be represented:

     GridView<Color> myGrid = new GridView<>(list);
     myGrid.setCellFactory(new Callback<GridView<Color>, GridCell<Color>>() {
         public GridCell<Color> call(GridView<Color> gridView) {
             return new ColorGridCell();
     Random r = new Random(System.currentTimeMillis());
     for(int i = 0; i < 500; i++) {
         list.add(new Color(r.nextDouble(), r.nextDouble(), r.nextDouble(), 1.0));
    See Also:
    • Constructor Detail

      • GridView

        public GridView()
        Creates a default, empty GridView control.
      • GridView

        public GridView​(ObservableList<T> items)
        Creates a default GridView control with the provided items prepopulated.
        items - The items to display inside the GridView.
    • Method Detail

      • setHorizontalCellSpacing

        public final void setHorizontalCellSpacing​(double value)
        Sets the amount of horizontal spacing there should be between cells in the same row.
        value - The amount of spacing to use.
      • getHorizontalCellSpacing

        public final double getHorizontalCellSpacing()
        Returns the amount of horizontal spacing there is between cells in the same row.
      • setVerticalCellSpacing

        public final void setVerticalCellSpacing​(double value)
        Sets the amount of vertical spacing there should be between cells in the same column.
        value - The amount of spacing to use.
      • getVerticalCellSpacing

        public final double getVerticalCellSpacing()
        Returns the amount of vertical spacing there is between cells in the same column.
      • setCellWidth

        public final void setCellWidth​(double value)
        Sets the width that all cells should be.
      • getCellWidth

        public final double getCellWidth()
        Returns the width that all cells should be.
      • setCellHeight

        public final void setCellHeight​(double value)
        Sets the height that all cells should be.
      • getCellHeight

        public final double getCellHeight()
        Returns the height that all cells should be.
      • cellFactoryProperty

        public final ObjectProperty<Callback<GridView<T>,​GridCell<T>>> cellFactoryProperty()
        Property representing the cell factory that is currently set in this GridView, or null if no cell factory has been set (in which case the default cell factory provided by the GridView skin will be used). The cell factory is used for instantiating enough GridCell instances for the visible area of the GridView. Refer to the GridView class documentation for more information and examples.
        See Also:
        getCellFactory(), setCellFactory(Callback)
      • setCellFactory

        public final void setCellFactory​(Callback<GridView<T>,​GridCell<T>> value)
        Sets the cell factory to use to create GridCell instances to show in the GridView.
      • getCellFactory

        public final Callback<GridView<T>,​GridCell<T>> getCellFactory()
        Returns the cell factory that will be used to create GridCell instances to show in the GridView.
      • itemsProperty

        public final ObjectProperty<ObservableList<T>> itemsProperty()
        The items to be displayed in the GridView (as rendered via GridCell instances). For example, if the ColorGridCell were being used (as in the case at the top of this class documentation), this items list would be populated with Color values. It is important to appreciate that the items list is used for the data, not the rendering. What is meant by this is that the items list should contain Color values, not the nodes that represent the Color. The actual rendering should be left up to the cell factory, where it will take the Color value and create / update the display as necessary.
        See Also:
        getItems(), setItems(ObservableList)
      • setItems

        public final void setItems​(ObservableList<T> value)
        Sets a new ObservableList as the items list underlying GridView. The old items list will be discarded.
      • getItems

        public final ObservableList<T> getItems()
        Returns the currently-in-use items list that is being used by the GridView.
      • getClassCssMetaData

        public static List<CssMetaData<? extends Styleable,​?>> getClassCssMetaData()
        The CssMetaData associated with this class, which may include the CssMetaData of its super classes.
      • getUserAgentStylesheet

        protected final String getUserAgentStylesheet​(Class<?> clazz,
                                                      String fileName)
        A helper method that ensures that the resource based lookup of the user agent stylesheet only happens once. Caches the external form of the resource.
        clazz - the class used for the resource lookup
        fileName - the name of the user agent stylesheet
        the external form of the user agent stylesheet (the path)