Class SearchBar

All Implemented Interfaces:
Accessible, Buildable, ConstraintTarget, Proxy

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

Reveals a search entry when search is started.

An example GtkSearchBar

It can also contain additional widgets, such as drop-down menus, or buttons. The search bar would appear when a search is started through typing on the keyboard, or the application’s search mode is toggled on.

For keyboard presses to start a search, the search bar must be told of a widget to capture key events from through setKeyCaptureWidget(Widget). This widget will typically be the top-level window, or a parent container of the search bar. Common shortcuts such as Ctrl+F should be handled as an application action, or through the menu items.

You will also need to tell the search bar about which entry you are using as your search entry using connectEntry(Editable).

Creating a search bar

The following example shows you how to create a more complex search entry.

A simple example

Shortcuts and Gestures

GtkSearchBar supports the following keyboard shortcuts:

  • Escape hides the search bar.

CSS nodes

searchbar
╰── revealer
    ╰── box
         ├── [child]
         ╰── [button.close]

GtkSearchBar has a main CSS node with name searchbar. It has a child node with name revealer that contains a node with name box. The box node contains both the CSS node of the child widget as well as an optional button node which gets the .close style class applied.

Accessibility

GtkSearchBar uses the Gtk.AccessibleRole.search role.

  • Constructor Details

    • SearchBar

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

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

    • getType

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

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

      protected SearchBar 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
    • connectEntry

      public void connectEntry(Editable entry)

      Connects the GtkEditable widget passed as the one to be used in this search bar.

      The entry should be a descendant of the search bar. Calling this function manually is only required if the entry isn’t the direct child of the search bar (as in our main example).

      Parameters:
      entry - a GtkEditable
    • getChild

      public @Nullable Widget getChild()
      Gets the child widget of bar.
      Returns:
      the child widget of this SearchBar
    • getKeyCaptureWidget

      public @Nullable Widget getKeyCaptureWidget()
      Gets the widget that this SearchBar is capturing key events from.
      Returns:
      The key capture widget.
    • getSearchMode

      public boolean getSearchMode()
      Returns whether the search mode is on or off.
      Returns:
      whether search mode is toggled on
    • getShowCloseButton

      public boolean getShowCloseButton()
      Returns whether the close button is shown.
      Returns:
      whether the close button is shown
    • setChild

      public void setChild(@Nullable Widget child)
      Sets the child widget of bar.
      Parameters:
      child - the child widget
    • setKeyCaptureWidget

      public void setKeyCaptureWidget(@Nullable Widget widget)

      Sets widget as the widget that this SearchBar will capture key events from.

      If key events are handled by the search bar, the bar will be shown, and the entry populated with the entered text.

      Note that despite the name of this function, the events are only 'captured' in the bubble phase, which means that editable child widgets of widget will receive text input before it gets captured. If that is not desired, you can capture and forward the events yourself with EventControllerKey.forward(Widget).

      Parameters:
      widget - a GtkWidget
    • setSearchMode

      public void setSearchMode(boolean searchMode)
      Switches the search mode on or off.
      Parameters:
      searchMode - the new state of the search mode
    • setShowCloseButton

      public void setShowCloseButton(boolean visible)

      Shows or hides the close button.

      Applications that already have a “search” toggle button should not show a close button in their search bar, as it duplicates the role of the toggle button.

      Parameters:
      visible - whether the close button will be shown or not
    • builder

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