Class Rating

  • All Implemented Interfaces:
    Styleable, EventTarget, Skinnable

    public class Rating
    extends Control
    A control for allowing users to provide a rating. This control supports partial ratings (i.e. not whole numbers and dependent upon where the user clicks in the control) and updating the rating on hover. Read on for more examples!

    Examples

    It can be hard to appreciate some of the features of the Rating control, so hopefully the following screenshots will help. Firstly, here is what the standard (horizontal) Rating control looks like when it has five stars, and a rating of two:
    Screenshot of horizontal Rating

    To create a Rating control that looks like this is simple:

     
     final Rating rating = new Rating();

    This creates a default horizontal rating control. To create a vertical Rating control, simply change the orientation, as such:

     
     final Rating rating = new Rating();
     rating.setOrientation(Orientation.VERTICAL);

    The end result of making this one line change is shown in the screenshot below:

    Screenshot of vertical Rating

    One of the features of the Rating control is that it doesn't just allow for 'integer' ratings: it also allows for the user to click anywhere within the rating area to set a 'float' rating. This is hard to describe, but easy to show in a picture:

    Screenshot of partial Rating

    In essence, in the screenshot above, the user clicked roughly in the middle of the third star. This results in a rating of approximately 2.44. To enable partial ratings, simply do the following when instantiating the Rating control:

     
     final Rating rating = new Rating();
     rating.setPartialRating(true);

    So far all of the Rating controls demonstrated above have required the user to click on the stars to register their rating. This may not be the preferred user interaction - often times the preferred approach is to simply allow for the rating to be registered by the user hovering their mouse over the rating stars. This mode is also supported by the Rating control, using the update on hover property, as such:

     
     final Rating rating = new Rating();
     rating.setUpdateOnHover(true);

    It is also allowable to have a Rating control that both updates on hover and allows for partial values: the 'golden fill' of the default graphics will automatically follow the users mouse as they move it along the Rating scale. To enable this, just set both properties to true.

    • Constructor Detail

      • Rating

        public Rating()
        Creates a default instance with a minimum rating of 0 and a maximum rating of 5.
      • Rating

        public Rating​(int max)
        Creates a default instance with a minimum rating of 0 and a maximum rating as provided by the argument.
        Parameters:
        max - The maximum allowed rating value.
      • Rating

        public Rating​(int max,
                      int rating)
        Creates a Rating instance with a minimum rating of 0, a maximum rating as provided by the max argument, and a current rating as provided by the rating argument.
        Parameters:
        max - The maximum allowed rating value.
    • Method Detail

      • setRating

        public final void setRating​(double value)
        Sets the current rating value.
      • getRating

        public final double getRating()
        Returns the current rating value.
      • setMax

        public final void setMax​(int value)
        Sets the maximum-allowed rating value.
      • getMax

        public final int getMax()
        Returns the maximum-allowed rating value.
      • setOrientation

        public final void setOrientation​(Orientation value)
        Sets the Orientation of the Rating - this can either be horizontal or vertical.
      • getOrientation

        public final Orientation getOrientation()
        Returns the Orientation of the Rating - this can either be horizontal or vertical.
      • partialRatingProperty

        public final BooleanProperty partialRatingProperty()
        If true this allows for users to set a rating as a floating point value. In other words, the range of the rating 'stars' can be thought of as a range between [0, max], and whereever the user clicks will be calculated as the new rating value. If this is false the more typical approach is used where the selected 'star' is used as the rating.
        See Also:
        isPartialRating(), setPartialRating(boolean)
      • setPartialRating

        public final void setPartialRating​(boolean value)
        Sets whether partial rating support is enabled or not.
      • isPartialRating

        public final boolean isPartialRating()
        Returns whether partial rating support is enabled or not.
      • updateOnHoverProperty

        public final BooleanProperty updateOnHoverProperty()
        If true this allows for the rating property to be updated simply by the user hovering their mouse over the control. If false the user is required to click on their preferred rating to register the new rating with this control.
        See Also:
        isUpdateOnHover(), setUpdateOnHover(boolean)
      • setUpdateOnHover

        public final void setUpdateOnHover​(boolean value)
        Sets whether update on hover support is enabled or not.
      • isUpdateOnHover

        public final boolean isUpdateOnHover()
        Returns whether update on hover support is enabled or not.
      • 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.
        Parameters:
        clazz - the class used for the resource lookup
        fileName - the name of the user agent stylesheet
        Returns:
        the external form of the user agent stylesheet (the path)