Class Button

All Implemented Interfaces:
Accessible, Actionable, Buildable, ConstraintTarget, Proxy
Direct Known Subclasses:
LinkButton, LockButton, StyleSchemeChooserButton, ToggleButton

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

Calls a callback function when the button is clicked.

An example GtkButton

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.

Shortcuts and Gestures

The following signals have default keybindings:

  • Gtk.Button::activate

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 ToggleButton, MenuButton, VolumeButton, LockButton, ColorButton or 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.AccessibleRole.button role.

  • Constructor Details

    • Button

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

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

    • getType

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

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

      protected Button 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
    • fromIconName

      public static Button fromIconName(String iconName)

      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:
      iconName - an icon name
      Returns:
      a new GtkButton displaying the themed icon
    • withLabel

      public static Button withLabel(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
    • withMnemonic

      public static Button withMnemonic(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
    • getCanShrink

      public boolean getCanShrink()
      Retrieves whether the button can be smaller than the natural size of its contents.
      Returns:
      true if the button can shrink, and false otherwise
      Since:
      4.12
    • getChild

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

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

      public @Nullable String getIconName()

      Returns the icon name of the button.

      If the icon name has not been set with setIconName(String) the return value will be null. This will be the case if you create an empty button with Button() to use as a container.

      Returns:
      The icon name set via setIconName(String)
    • getLabel

      public @Nullable String getLabel()

      Fetches the text from the label of the button.

      If the label text has not been set with setLabel(String) the return value will be null. This will be the case if you create an empty button with Button() 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 setUseUnderline(boolean).

      Returns:
      true if an embedded underline in the button label indicates the mnemonic accelerator keys.
    • setCanShrink

      public void setCanShrink(boolean canShrink)

      Sets whether the button size can be smaller than the natural size of its contents.

      For text buttons, setting canShrink to true will ellipsize the label.

      For icons and custom children, this function has no effect.

      Parameters:
      canShrink - whether the button can shrink
      Since:
      4.12
    • 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 this 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 hasFrame)

      Sets the style of the button.

      Buttons can have a flat appearance or have a frame drawn around them.

      Parameters:
      hasFrame - whether the button should have a visible frame
    • setIconName

      public void setIconName(String iconName)

      Adds a GtkImage with the given icon name as a child.

      If this Button already contains a child widget, that child widget will be removed and replaced with the image.

      Parameters:
      iconName - An icon name
    • 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 useUnderline)

      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:
      useUnderline - true if underlines in the text indicate mnemonics
    • activate

      protected void activate()
      Signal that causes the button to animate press then release. Applications should never connect to this signal, but use the clicked signal.
    • clicked

      protected void clicked()
      Signal emitted when the button has been activated (pressed and released).
    • onActivate

      Emitted to animate press then release.

      This is an action signal. Applications should never connect to this signal, but use the Gtk.Button::clicked signal.

      The default bindings for this signal are all forms of the and Enter keys.

      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(Button.ActivateCallback).
    • onClicked

      Emitted when the button has been activated (pressed and released).
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitClicked

      public void emitClicked()
      Emits the "clicked" signal. See onClicked(Button.ClickedCallback).
    • builder

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