Class Button

  • All Implemented Interfaces:
    Clickable

    public class Button
    extends Label
    implements Clickable
    A button is a widget that displays a text and reacts to click events.

    This example shows a simple button:

     Button button = new Button("Press Me");
     
    Simple button.

    This example shows a styled button:

     Button button = new Button("Press Me");
    
     CascadingStylesheet stylesheet = new CascadingStylesheet();
     desktop.setStylesheet(stylesheet);
    
     EditableStyle buttonStyle = stylesheet.getSelectorStyle(new TypeSelector(Button.class));
     buttonStyle.setBackground(new RectangularBackground(Colors.SILVER));
     buttonStyle.setBorder(new RectangularBorder(Colors.BLACK, 1));
     buttonStyle.setFont(Font.getFont("/fonts/source_sans_pro_24.ejf"));
     
    Styled button.

    This example shows a styled button with its background color changing on a press action:

     Button button = new Button("Press Me");
    
     CascadingStylesheet stylesheet = new CascadingStylesheet();
     desktop.setStylesheet(stylesheet);
    
     EditableStyle buttonStyle = stylesheet.getSelectorStyle(new TypeSelector(Button.class));
     buttonStyle.setBackground(new RectangularBackground(Colors.SILVER));
     buttonStyle.setBorder(new RectangularBorder(Colors.BLACK, 1));
     buttonStyle.setFont(Font.getFont("/fonts/source_sans_pro_24.ejf"));
    
     EditableStyle pressedButtonStyle = stylesheet
                    .getSelectorStyle(new AndCombinator(new TypeSelector(Button.class), new StateSelector(StateSelector.ACTIVE)));
     pressedButtonStyle.setBackground(new RectangularBackground(Colors.GRAY));
     
    Styled pressed button.
    Since:
    2.3.0
    • Constructor Detail

      • Button

        public Button()
        Creates a button with an empty text.
      • Button

        public Button​(String text)
        Creates a button with the given text to display.
        Parameters:
        text - the text to display.
    • Method Detail

      • setOnClickListener

        public void setOnClickListener​(@Nullable
                                       OnClickListener listener)
        Sets the listener on the click events of this button.
        Parameters:
        listener - the listener to set.
      • isInState

        public boolean isInState​(int state)
        Description copied from class: Widget
        Gets whether or not the widget is in the given state.
        Overrides:
        isInState in class Widget
        Parameters:
        state - the state to check.
        Returns:
        true if the widget is in the given state, false otherwise.
      • handleEvent

        public boolean handleEvent​(int event)
        Description copied from class: Widget
        Handles the given event. Does nothing by default and returns false (does not consume event).

        Called by the desktop event manager.

        Overrides:
        handleEvent in class Widget
        Parameters:
        event - the event to handle.
        Returns:
        true if the widget has consumed the event, false otherwise.
      • setPressed

        public void setPressed​(boolean pressed)
        Description copied from interface: Clickable
        Changes the clickable element state.
        Specified by:
        setPressed in interface Clickable
        Parameters:
        pressed - true if the element is pressed, false otherwise.