Class Button

All Implemented Interfaces:
PointerInterface
Direct Known Subclasses:
LinkButton, LockButton, ToggleButton

public class Button extends Widget
The `GtkButton` widget is generally used to trigger a callback function that is
called when the button is pressed.

![An example GtkButton](button.png)

The `GtkButton` widget can hold any valid child widget. That is, it can hold
almost any other standard `GtkWidget`. The most commonly used child is the
`GtkLabel`.

# CSS nodes

`GtkButton` has a single CSS node with name button. The node will get the
style classes .image-button or .text-button, if the content is just an
image or label, respectively. It may also receive the .flat style class.
When activating a button via the keyboard, the button will temporarily
gain the .keyboard-activating style class.

Other style classes that are commonly used with `GtkButton` include
.suggested-action and .destructive-action. In special cases, buttons
can be made round by adding the .circular style class.

Button-like widgets like [class@Gtk.ToggleButton], [class@Gtk.MenuButton],
[class@Gtk.VolumeButton], [class@Gtk.LockButton], [class@Gtk.ColorButton]
or [class@Gtk.FontButton] use style classes such as .toggle, .popup, .scale,
.lock, .color on the button node to differentiate themselves from a plain
`GtkButton`.

# Accessibility

`GtkButton` uses the %GTK_ACCESSIBLE_ROLE_BUTTON role.

https://docs.gtk.org/gtk4/class.Button.html

  • Field Details

  • Constructor Details

    • Button

      public Button(PointerContainer pointer)
    • Button

      public Button()
      Creates a new `GtkButton` widget.

      To add a child widget to the button, use [method@Gtk.Button.set_child].
  • Method Details

    • getClassHandler

      public static ClassHandler getClassHandler()
    • newFromIconNameButton

      public static Button newFromIconNameButton(@Nullable Str icon_name)
      Creates a new button containing an icon from the current icon theme.

      If the icon name isn’t known, a “broken image” icon will be
      displayed instead. If the current icon theme is changed, the icon
      will be updated appropriately.
      Parameters:
      icon_name - an icon name
      Returns:
      a new `GtkButton` displaying the themed icon
    • newFromIconNameButton

      public static Button newFromIconNameButton(String icon_name)
      Creates a new button containing an icon from the current icon theme.

      If the icon name isn’t known, a “broken image” icon will be
      displayed instead. If the current icon theme is changed, the icon
      will be updated appropriately.
      Parameters:
      icon_name - an icon name
      Returns:
      a new `GtkButton` displaying the themed icon
    • newWithLabelButton

      public static Button newWithLabelButton(@Nonnull Str label)
      Creates a `GtkButton` widget with a `GtkLabel` child.
      Parameters:
      label - The text you want the `GtkLabel` to hold
      Returns:
      The newly created `GtkButton` widget
    • newWithLabelButton

      public static Button newWithLabelButton(String label)
      Creates a `GtkButton` widget with a `GtkLabel` child.
      Parameters:
      label - The text you want the `GtkLabel` to hold
      Returns:
      The newly created `GtkButton` widget
    • newWithMnemonicButton

      public static Button newWithMnemonicButton(@Nonnull Str label)
      Creates a new `GtkButton` containing a label.

      If characters in @label are preceded by an underscore, they are underlined.
      If you need a literal underscore character in a label, use “__” (two
      underscores). The first underlined character represents a keyboard
      accelerator called a mnemonic. Pressing Alt and that key activates the button.
      Parameters:
      label - The text of the button, with an underscore in front of the mnemonic character
      Returns:
      a new `GtkButton`
    • newWithMnemonicButton

      public static Button newWithMnemonicButton(String label)
      Creates a new `GtkButton` containing a label.

      If characters in @label are preceded by an underscore, they are underlined.
      If you need a literal underscore character in a label, use “__” (two
      underscores). The first underlined character represents a keyboard
      accelerator called a mnemonic. Pressing Alt and that key activates the button.
      Parameters:
      label - The text of the button, with an underscore in front of the mnemonic character
      Returns:
      a new `GtkButton`
    • getChild

      public Widget getChild()
      Gets the child widget of @button.
      Returns:
      the child widget of @button
    • getHasFrame

      public boolean getHasFrame()
      Returns whether the button has a frame.
      Returns:
      %TRUE if the button has a frame
    • getIconName

      public Str getIconName()
      Returns the icon name of the button.

      If the icon name has not been set with [method@Gtk.Button.set_icon_name]
      the return value will be %NULL. This will be the case if you create
      an empty button with [ctor@Gtk.Button.new] to use as a container.
      Returns:
      The icon name set via [method@Gtk.Button.set_icon_name]
    • getLabel

      public Str getLabel()
      Fetches the text from the label of the button.

      If the label text has not been set with [method@Gtk.Button.set_label]
      the return value will be %NULL. This will be the case if you create
      an empty button with [ctor@Gtk.Button.new] to use as a container.
      Returns:
      The text of the label widget. This string is owned by the widget and must not be modified or freed.
    • getUseUnderline

      public boolean getUseUnderline()
      gets whether underlines are interpreted as mnemonics.

      See [method@Gtk.Button.set_use_underline].
      Returns:
      %TRUE if an embedded underline in the button label indicates the mnemonic accelerator keys.
    • setChild

      public void setChild(@Nullable Widget child)
      Sets the child widget of @button.

      Note that by using this API, you take full responsibility for setting
      up the proper accessibility label and description information for @button.
      Most likely, you'll either set the accessibility label or description
      for @button explicitly, or you'll set a labelled-by or described-by
      relations from @child to @button.
      Parameters:
      child - the child widget
    • setHasFrame

      public void setHasFrame(boolean has_frame)
      Sets the style of the button.

      Buttons can has a flat appearance or have a frame drawn around them.
      Parameters:
      has_frame - whether the button should have a visible frame
    • setIconName

      public void setIconName(@Nonnull Str icon_name)
      Adds a `GtkImage` with the given icon name as a child.

      If @button already contains a child widget, that child widget will
      be removed and replaced with the image.
      Parameters:
      icon_name - An icon name
    • setIconName

      public void setIconName(String icon_name)
      Adds a `GtkImage` with the given icon name as a child.

      If @button already contains a child widget, that child widget will
      be removed and replaced with the image.
      Parameters:
      icon_name - An icon name
    • setLabel

      public void setLabel(@Nonnull Str label)
      Sets the text of the label of the button to @label.

      This will also clear any previously set labels.
      Parameters:
      label - a string
    • setLabel

      public void setLabel(String label)
      Sets the text of the label of the button to @label.

      This will also clear any previously set labels.
      Parameters:
      label - a string
    • setUseUnderline

      public void setUseUnderline(boolean use_underline)
      Sets whether to use underlines as mnemonics.

      If true, an underline in the text of the button label indicates
      the next character should be used for the mnemonic accelerator key.
      Parameters:
      use_underline - %TRUE if underlines in the text indicate mnemonics
    • onActivate

      public SignalHandler onActivate(Button.OnActivate signal)
      Connect to signal "activate".
      See Button.OnActivate.onActivate() for signal description.
      Field SIGNAL_ON_ACTIVATE contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • onClicked

      public SignalHandler onClicked(Button.OnClicked signal)
      Connect to signal "clicked".
      See Button.OnClicked.onClicked() for signal description.
      Field SIGNAL_ON_CLICKED contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • asAccessible

      public Accessible asAccessible()
      Implements interface Accessible. Call this to get access to interface functions.
      Overrides:
      asAccessible in class Widget
      Returns:
      Accessible
    • asActionable

      public Actionable asActionable()
      Implements interface Actionable. Call this to get access to interface functions.
      Returns:
      Actionable
    • asBuildable

      public Buildable asBuildable()
      Implements interface Buildable. Call this to get access to interface functions.
      Overrides:
      asBuildable in class Widget
      Returns:
      Buildable
    • asConstraintTarget

      public ConstraintTarget asConstraintTarget()
      Implements interface ConstraintTarget. Call this to get access to interface functions.
      Overrides:
      asConstraintTarget in class Widget
      Returns:
      ConstraintTarget
    • getTypeID

      public static long getTypeID()
    • getParentTypeID

      public static long getParentTypeID()
    • getTypeSize

      public static TypeSystem.TypeSize getTypeSize()
    • getParentTypeSize

      public static TypeSystem.TypeSize getParentTypeSize()
    • getInstanceSize

      public static int getInstanceSize()