Class Switch

All Implemented Interfaces:
Accessible, Actionable, Buildable, ConstraintTarget, Proxy

@Generated("org.javagi.JavaGI") public class Switch extends Widget implements Accessible, Actionable, Buildable, ConstraintTarget

Shows a "light switch" that has two states: on or off.

An example GtkSwitch

The user can control which state should be active by clicking the empty area, or by dragging the slider.

GtkSwitch can also express situations where the underlying state changes with a delay. In this case, the slider position indicates the user's recent change (represented by the Gtk.Switch:active property), while the trough color indicates the present underlying state (represented by the Gtk.Switch:state property).

GtkSwitch with delayed state change

See Gtk.Switch::state-set for details.

Shortcuts and Gestures

GtkSwitch supports pan and drag gestures to move the slider.

CSS nodes

switch
├── image
├── image
╰── slider

GtkSwitch has four css nodes, the main node with the name switch and subnodes for the slider and the on and off images. Neither of them is using any style classes.

Accessibility

GtkSwitch uses the Gtk.AccessibleRole.switch role.

  • Constructor Details

    • Switch

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

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

    • getType

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

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

      protected Switch 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
    • getActive

      public boolean getActive()
      Gets whether the GtkSwitch is in its “on” or “off” state.
      Returns:
      true if the GtkSwitch is active, and false otherwise
    • getState

      public boolean getState()
      Gets the underlying state of the GtkSwitch.
      Returns:
      the underlying state
    • setActive

      public void setActive(boolean isActive)
      Changes the state of this Switch to the desired one.
      Parameters:
      isActive - true if this Switch should be active, and false otherwise
    • setState

      public void setState(boolean state)

      Sets the underlying state of the GtkSwitch.

      This function is typically called from a Gtk.Switch::state-set signal handler in order to set up delayed state changes.

      See Gtk.Switch::state-set for details.

      Parameters:
      state - the new state
    • onActivate

      Emitted to animate the switch.

      Applications should never connect to this signal, but use the Gtk.Switch:active property.

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

      public void emitActivate()
      Emits the "activate" signal. See onActivate(Switch.ActivateCallback).
    • onStateSet

      Emitted to change the underlying state.

      The ::state-set signal is emitted when the user changes the switch position. The default handler calls setState(boolean) with the value of state.

      To implement delayed state change, applications can connect to this signal, initiate the change of the underlying state, and call setState(boolean) when the underlying state change is complete. The signal handler should return true to prevent the default handler from running.

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

      public boolean emitStateSet(boolean state)
      Emits the "state-set" signal. See onStateSet(Switch.StateSetCallback).
    • builder

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