Class RatingBar

java.lang.Object
com.codename1.ui.Component
com.codename1.components.RatingBar
All Implemented Interfaces:
Animation, Editable, StyleListener

public class RatingBar extends Component

A row of stars that shows a rating and lets the user pick one by tapping or dragging across it.

RatingBar rating = new RatingBar(5);
rating.setValue(3.5f);
rating.addActionListener(e -> Log.p("Rated " + rating.getValue()));

The value runs from 0 to the number of stars. It is shown to any fraction, so an average of 4.3 paints 4.3 stars; what the user picks is rounded to the step, whole stars by default and halves with setStep(0.5f). setEditable(false) makes it display only. The filled stars use the accent colour of the UIID RatingBar (its foreground when themed); the empty ones a faded track colour. Left and right keys change the value by one step when it has focus.

  • Constructor Details

    • RatingBar

      public RatingBar()
      A bar of five stars.
    • RatingBar

      public RatingBar(int stars)

      A bar with a given number of stars.

      Parameters
      • stars: the number of stars, at least one
  • Method Details

    • getStars

      public int getStars()

      The number of stars.

      Returns

      the maximum value

    • getValue

      public float getValue()

      The rating.

      Returns

      a value from 0 to #getStars()

    • setValue

      public void setValue(float value)

      Sets the rating without notifying listeners. Any fraction is shown as it is.

      Parameters
      • value: the rating, clamped to 0..stars
    • setStep

      public void setStep(float step)

      Sets the granularity of what the user picks.

      Parameters
      • step: 1 for whole stars, 0.5 for halves; must be positive
    • getStep

      public float getStep()

      The granularity of what the user picks.

      Returns

      the step

    • setEditable

      public void setEditable(boolean editable)

      Whether the user can change the rating.

      Parameters
      • editable: false to make the bar display only
    • isEditable

      public boolean isEditable()

      Whether the user can change the rating.

      Returns

      true unless #setEditable(boolean) turned it off

      Specified by:
      isEditable in interface Editable
      Overrides:
      isEditable in class Component
    • addActionListener

      public void addActionListener(ActionListener l)

      Listens for the user changing the rating.

      Parameters
      • l: the listener
    • removeActionListener

      public void removeActionListener(ActionListener l)

      Removes a listener.

      Parameters
      • l: the listener
    • pointerPressed

      public void pointerPressed(int x, int y)
      Description copied from class: Component

      If this Component is focused, the pointer pressed event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerPressed in class Component
    • pointerDragged

      public void pointerDragged(int x, int y)
      Description copied from class: Component

      If this Component is focused, the pointer dragged event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerDragged in class Component
    • keyPressed

      public void keyPressed(int keyCode)
      Left and right move the rating while the bar has focus, rather than the focus; up and down still leave it.
      Overrides:
      keyPressed in class Component
    • keyReleased

      public void keyReleased(int keyCode)
      Description copied from class: Component

      If this Component is focused, the key released event will call this method

      Parameters
      • keyCode: the key code value to indicate a physical key.
      Overrides:
      keyReleased in class Component
    • isStickyDrag

      protected boolean isStickyDrag()
      Description copied from class: Component

      Returns true if the component is interested in receiving drag/pointer release events even after the gesture exceeded its boundaries. This is useful for spinners etc. where the motion might continue beyond the size of the component

      Returns

      false by default

      Overrides:
      isStickyDrag in class Component
    • calcPreferredSize

      protected Dimension calcPreferredSize()
      Description copied from class: Component

      Calculates the preferred size based on component content. This method is invoked lazily by getPreferred size.

      Returns

      the calculated preferred size based on component content

      Overrides:
      calcPreferredSize in class Component
    • paint

      public void paint(Graphics g)
      Description copied from class: Component

      This method paints the Component on the screen, it should be overriden by subclasses to perform custom drawing or invoke the UI API's to let the PLAF perform the rendering.

      Parameters
      • g: the component graphics
      Specified by:
      paint in interface Animation
      Overrides:
      paint in class Component
    • getAccessibilityText

      public String getAccessibilityText()
      Description copied from class: Component

      Returns the text that describes this component to assistive technologies. If no text was set explicitly, this method attempts to derive a description from the component's label or content.

      Returns

      accessibility description or null if none

      Overrides:
      getAccessibilityText in class Component