Class SliderField

java.lang.Object
ca.corbett.forms.fields.FormField
ca.corbett.forms.fields.SliderField

public class SliderField extends FormField
Wraps a JSlider and provides options for configuring it. This implementation goes beyond the functionality offered by the Java Swing JSlider component. With SliderField, you can optionally specify a set of color stops, and the slider will change color based on the slider's current position. There are some built-in color stops that you can use, or you can supply your own. The SliderField can also optionally show its current value in a field label, either using the numeric value of the current slider position, or by showing a caller-supplied String associated with a particular range of values.

Note: Only horizontal sliders are supported. (This is not a technical restriction, but more of a form real estate restriction, because FormPanels are laid out vertically, and a vertical slider would take up more vertical space than is reasonable).

Since:
swing-extras 2.5
Author:
scorbo2
  • Constructor Details

    • SliderField

      public SliderField(String labelText, int min, int max, int value)
  • Method Details

    • setColorStops

      public SliderField setColorStops(List<Color> colorStops)
      Optionally set a list of color stops for this slider. If set, the color of the slider will change along the given color gradation. For example:
      setColorStops(List.of(Color.RED, Color.YELLOW, Color.GREEN));
      will show a slider that is shaded red on the left side, yellow towards the middle, and green towards the right side of the slider. This can be a good way to make the slider more visually meaningful depending on the value it represents. If not set (which is the default state), the slider will appear as a regular JSlider. Note that you will have problems if the number of color stops exceeds the range of values allowed in the slider (some colors will be dropped).
    • setLabels

      public SliderField setLabels(List<String> labels, boolean alsoShowNumericValue)
      Optionally set human-readable labels to be used in place of (or alongside) the numeric value label. Only applies if the value label is showing! The value label may be hidden with setShowValueLabel(false). If setColorStops() has not also been invoked, a default color pattern will be applied automatically. You can change the default color pattern by invoking setColorStops().

      Note that the size of the label list doesn't necessarily have to match the size of the color stop list (though that usually makes sense). You can have more or fewer labels in this list than there are color stops, and the labels will be interpreted accordingly. For example, if you supply a list of four labels, the first label will apply to the left 25% of the slider track, the second will apply to the next 25%, and so on, regardless of the color gradation within the slider. Note that you will have problems if the number of labels exceeds the range of values allowed in the slider (some labels will be dropped).

    • setShowValueLabel

      public SliderField setShowValueLabel(boolean show)
      Shows or hides the value label underneath the JSlider that gives a text representation of the current value of the slider.
    • setShouldExpand

      public SliderField setShouldExpand(boolean should)
    • shouldExpand

      public boolean shouldExpand()
      Description copied from class: FormField
      By default, FormPanel will allocate only the space that the field component requires. Descendant classes can override the default false value here to indicate that their field component should be allowed to expand as much space as is available to it. For example: PanelField.
      Overrides:
      shouldExpand in class FormField
    • setEnabled

      public FormField setEnabled(boolean isEnabled)
      Description copied from class: FormField
      Enables or disables all components in this field.

      Note: The help label is intentionally not disabled when the field is disabled. This is because the help label should always be visible and accessible to the user, even when the field itself is disabled. Descendant classes can override setEnabled() to implement their own handling if needed.

      Overrides:
      setEnabled in class FormField
      Parameters:
      isEnabled - whether to enable or disable the components.
    • setValue

      public SliderField setValue(int value)
    • getValue

      public int getValue()
    • setDefaultBorder

      public void setDefaultBorder()
      Sets a default line border around the slider in this FormField. You can also invoke setBorder to set some custom border.
    • setBorder

      public SliderField setBorder(Border border)
      Sets the given border around the JSlider in this FormField. You can also invoke setDefaultBorder() to easily set a simple line border.
    • isIsDefaultBorderEnabled

      public static boolean isIsDefaultBorderEnabled()
      Reports whether a default border will be added to all new SliderField instances automatically.
    • setIsDefaultBorderEnabled

      public static void setIsDefaultBorderEnabled(boolean enable)
      By default, all new instances of this class will give themselves a default LineBorder. You can disable that behavior with this method. Note that this only affects new instance creation from this point on - it will not change the border of any already-created instances.