Class SpinButton

All Implemented Interfaces:
Accessible, AccessibleRange, Buildable, CellEditable, ConstraintTarget, Editable, Orientable, Proxy

@Generated("org.javagi.JavaGI") public class SpinButton extends Widget implements Accessible, AccessibleRange, Buildable, CellEditable, ConstraintTarget, Editable, Orientable

Allows to enter or change numeric values.

An example GtkSpinButton

Rather than having to directly type a number into a GtkEntry, GtkSpinButton allows the user to click on one of two arrows to increment or decrement the displayed value. A value can still be typed in, with the bonus that it can be checked to ensure it is in a given range.

The main properties of a GtkSpinButton are through an adjustment. See the Adjustment documentation for more details about an adjustment's properties.

Note that GtkSpinButton will by default make its entry large enough to accommodate the lower and upper bounds of the adjustment. If this is not desired, the automatic sizing can be turned off by explicitly setting Gtk.Editable:width-chars to a value != -1.

Using a GtkSpinButton to get an integer

// Provides a function to retrieve an integer value from a GtkSpinButton
// and creates a spin button to model percentage values.

int
grab_int_value (GtkSpinButton *button,
                gpointer       user_data)
{
  return gtk_spin_button_get_value_as_int (button);
}

void
create_integer_spin_button (void)
{

  GtkWidget *window, *button;
  GtkAdjustment *adjustment;

  adjustment = gtk_adjustment_new (50.0, 0.0, 100.0, 1.0, 5.0, 0.0);

  window = gtk_window_new ();

  // creates the spinbutton, with no decimal places
  button = gtk_spin_button_new (adjustment, 1.0, 0);
  gtk_window_set_child (GTK_WINDOW (window), button);

  gtk_window_present (GTK_WINDOW (window));
}

Using a GtkSpinButton to get a floating point value

// Provides a function to retrieve a floating point value from a
// GtkSpinButton, and creates a high precision spin button.

float
grab_float_value (GtkSpinButton *button,
                  gpointer       user_data)
{
  return gtk_spin_button_get_value (button);
}

void
create_floating_spin_button (void)
{
  GtkWidget *window, *button;
  GtkAdjustment *adjustment;

  adjustment = gtk_adjustment_new (2.500, 0.0, 5.0, 0.001, 0.1, 0.0);

  window = gtk_window_new ();

  // creates the spinbutton, with three decimal places
  button = gtk_spin_button_new (adjustment, 0.001, 3);
  gtk_window_set_child (GTK_WINDOW (window), button);

  gtk_window_present (GTK_WINDOW (window));
}

Shortcuts and Gestures

The following signals have default keybindings:

  • Gtk.SpinButton::change-value

CSS nodes

spinbutton.horizontal
├── text
│    ├── undershoot.left
│    ╰── undershoot.right
├── button.down
╰── button.up
spinbutton.vertical
├── button.up
├── text
│    ├── undershoot.left
│    ╰── undershoot.right
╰── button.down

GtkSpinButtons main CSS node has the name spinbutton. It creates subnodes for the entry and the two buttons, with these names. The button nodes have the style classes .up and .down. The GtkText subnodes (if present) are put below the text node. The orientation of the spin button is reflected in the .vertical or .horizontal style class on the main node.

Accessibility

GtkSpinButton uses the Gtk.AccessibleRole.spin_button role.

  • Constructor Details

    • SpinButton

      public SpinButton(MemorySegment address)
      Create a SpinButton instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • SpinButton

      public SpinButton(@Nullable Adjustment adjustment, double climbRate, int digits)
      Creates a new GtkSpinButton.
      Parameters:
      adjustment - the GtkAdjustment that this spin button should use
      climbRate - specifies by how much the rate of change in the value will accelerate if you continue to hold down an up/down button or arrow key
      digits - the number of decimal places to display
    • SpinButton

      public SpinButton()
      Create a new SpinButton.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the SpinButton class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected SpinButton asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class Widget
      Returns:
      the instance as if it were its parent type
    • withRange

      public static SpinButton withRange(double min, double max, double step)

      Creates a new GtkSpinButton with the given properties.

      This is a convenience constructor that allows creation of a numeric GtkSpinButton without manually creating an adjustment. The value is initially set to the minimum value and a page increment of 10 * step is the default. The precision of the spin button is equivalent to the precision of step.

      Note that the way in which the precision is derived works best if step is a power of ten. If the resulting precision is not suitable for your needs, use setDigits(int) to correct it.

      Parameters:
      min - Minimum allowable value
      max - Maximum allowable value
      step - Increment added or subtracted by spinning the widget
      Returns:
      The new GtkSpinButton
    • configure

      public void configure(@Nullable Adjustment adjustment, double climbRate, int digits)

      Changes the properties of an existing spin button.

      The adjustment, climb rate, and number of decimal places are updated accordingly.

      Parameters:
      adjustment - a GtkAdjustment to replace the spin button’s existing adjustment, or null to leave its current adjustment unchanged
      climbRate - the new climb rate
      digits - the number of decimal places to display in the spin button
    • getActivatesDefault

      public boolean getActivatesDefault()
      Retrieves the value set by setActivatesDefault(boolean).
      Returns:
      true if the spin button will activate the default widget
      Since:
      4.14
    • getAdjustment

      public Adjustment getAdjustment()
      Get the adjustment associated with a GtkSpinButton.
      Returns:
      the GtkAdjustment of this SpinButton
    • getClimbRate

      public double getClimbRate()
      Returns the acceleration rate for repeated changes.
      Returns:
      the acceleration rate
    • getDigits

      public int getDigits()
      Fetches the precision of spinButton.
      Returns:
      the current precision
    • getIncrements

      public void getIncrements(@Nullable Out<Double> step, @Nullable Out<Double> page)

      Gets the current step and page the increments used by spinButton.

      See setIncrements(double, double).

      Parameters:
      step - location to store step increment
      page - location to store page increment
    • getNumeric

      public boolean getNumeric()
      Returns whether non-numeric text can be typed into the spin button.
      Returns:
      true if only numeric text can be entered
    • getRange

      public void getRange(@Nullable Out<Double> min, @Nullable Out<Double> max)

      Gets the range allowed for spinButton.

      See setRange(double, double).

      Parameters:
      min - location to store minimum allowed value
      max - location to store maximum allowed value
    • getSnapToTicks

      public boolean getSnapToTicks()
      Returns whether the values are corrected to the nearest step.
      Returns:
      true if values are snapped to the nearest step
    • getUpdatePolicy

      public SpinButtonUpdatePolicy getUpdatePolicy()

      Gets the update behavior of a spin button.

      See setUpdatePolicy(SpinButtonUpdatePolicy).

      Returns:
      the current update policy
    • getValue

      public double getValue()
      Get the value in the spinButton.
      Returns:
      the value of this SpinButton
    • getValueAsInt

      public int getValueAsInt()
      Get the value this SpinButton represented as an integer.
      Returns:
      the value of this SpinButton
    • getWrap

      public boolean getWrap()
      Returns whether the spin button’s value wraps around to the opposite limit when the upper or lower limit of the range is exceeded.
      Returns:
      true if the spin button wraps around
    • setActivatesDefault

      public void setActivatesDefault(boolean activatesDefault)

      Sets whether activating the spin button will activate the default widget for the window containing the spin button.

      See Gtk.SpinButton::activate for what counts as activation.

      Parameters:
      activatesDefault - true to activate window’s default widget on activation
      Since:
      4.14
    • setAdjustment

      public void setAdjustment(Adjustment adjustment)
      Replaces the GtkAdjustment associated with spinButton.
      Parameters:
      adjustment - a GtkAdjustment to replace the existing adjustment
    • setClimbRate

      public void setClimbRate(double climbRate)
      Sets the acceleration rate for repeated changes when you hold down a button or key.
      Parameters:
      climbRate - the rate of acceleration, must be >= 0
    • setDigits

      public void setDigits(int digits)

      Set the precision to be displayed by spinButton.

      Up to 20 digit precision is allowed.

      Parameters:
      digits - the number of digits after the decimal point to be displayed for the spin button’s value
    • setIncrements

      public void setIncrements(double step, double page)

      Sets the step and page increments for spin_button.

      This affects how quickly the value changes when the spin button’s arrows are activated.

      Parameters:
      step - increment applied for a button 1 press.
      page - increment applied for a button 2 press.
    • setNumeric

      public void setNumeric(boolean numeric)
      Sets the flag that determines if non-numeric text can be typed into the spin button.
      Parameters:
      numeric - flag indicating if only numeric entry is allowed
    • setRange

      public void setRange(double min, double max)

      Sets the minimum and maximum allowable values for spinButton.

      If the current value is outside this range, it will be adjusted to fit within the range, otherwise it will remain unchanged.

      Parameters:
      min - minimum allowable value
      max - maximum allowable value
    • setSnapToTicks

      public void setSnapToTicks(boolean snapToTicks)
      Sets the policy as to whether values are corrected to the nearest step increment when a spin button is activated after providing an invalid value.
      Parameters:
      snapToTicks - a flag indicating if invalid values should be corrected
    • setUpdatePolicy

      public void setUpdatePolicy(SpinButtonUpdatePolicy policy)

      Sets the update behavior of a spin button.

      This determines whether the spin button is always updated or only when a valid value is set.

      Parameters:
      policy - a GtkSpinButtonUpdatePolicy value
    • setValue

      public void setValue(double value)
      Sets the value of spinButton.
      Parameters:
      value - the new value
    • setWrap

      public void setWrap(boolean wrap)
      Sets the flag that determines if a spin button value wraps around to the opposite limit when the upper or lower limit of the range is exceeded.
      Parameters:
      wrap - a flag indicating if wrapping behavior is performed
    • spin

      public void spin(SpinType direction, double increment)
      Increment or decrement a spin button’s value in a specified direction by a specified amount.
      Parameters:
      direction - a GtkSpinType indicating the direction to spin
      increment - step increment to apply in the specified direction
    • update

      public void update()
      Manually force an update of the spin button.
    • onActivate

      Emitted when the spin button is activated.

      The keybindings for this signal are all forms of the Enter key.

      If the Enter key results in the value being committed to the spin button, then activation does not occur until Enter is pressed again.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      Since:
      4.14
      See Also:
    • emitActivate

      public void emitActivate()
      Emits the "activate" signal. See onActivate(SpinButton.ActivateCallback).
    • onChangeValue

      Emitted when the user initiates a value change.

      This is a keybinding signal.

      Applications should not connect to it, but may emit it with g_signal_emit_by_name() if they need to control the cursor programmatically.

      The default bindings for this signal are Up/Down and PageUp/PageDown.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitChangeValue

      public void emitChangeValue(ScrollType scroll)
      Emits the "change-value" signal. See onChangeValue(SpinButton.ChangeValueCallback).
    • onInput

      Emitted to convert the users input into a double value.

      The signal handler is expected to use Editable.getText() to retrieve the text of the spinbutton and set newValue to the new value.

      The default conversion uses g_strtod().

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitInput

      public int emitInput(Out<Double> newValue)
      Emits the "input" signal. See onInput(SpinButton.InputCallback).
    • onOutput

      Emitted to tweak the formatting of the value for display.

      // show leading zeros
      static gboolean
      on_output (GtkSpinButton *spin,
                 gpointer       data)
      {
         char *text;
         int value;
      
         value = gtk_spin_button_get_value_as_int (spin);
         text = g_strdup_printf ("%02d", value);
         gtk_editable_set_text (GTK_EDITABLE (spin), text):
         g_free (text);
      
         return TRUE;
      }
      
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitOutput

      public boolean emitOutput()
      Emits the "output" signal. See onOutput(SpinButton.OutputCallback).
    • onValueChanged

      Emitted when the value is changed.

      Also see the Gtk.SpinButton::output signal.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitValueChanged

      public void emitValueChanged()
      Emits the "value-changed" signal. See onValueChanged(SpinButton.ValueChangedCallback).
    • onWrapped

      Emitted right after the spinbutton wraps from its maximum to its minimum value or vice-versa.
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitWrapped

      public void emitWrapped()
      Emits the "wrapped" signal. See onWrapped(SpinButton.WrappedCallback).
    • builder

      public static SpinButton.Builder<? extends SpinButton.Builder> builder()
      A SpinButton.Builder object constructs a SpinButton with the specified properties. Use the various set...() methods to set properties, and finish construction with SpinButton.Builder.build().
      Returns:
      the builder object