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(States.ACTIVE)));
     pressedButtonStyle.setBackground(new RectangularBackground(Colors.GRAY));
     
    Styled pressed button.
    Since:
    2.3.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int ACTIVE
      Active state.
      • Fields inherited from class ej.mwt.Widget

        NO_CONSTRAINT
    • Constructor Summary

      Constructors 
      Constructor Description
      Button()
      Creates a button with an empty text.
      Button​(java.lang.String text)
      Creates a button with the given text to display.
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      void handleClick()
      Deprecated.
      Internal API.
      boolean handleEvent​(int event)  
      boolean isInState​(int state)  
      void setOnClickListener​(OnClickListener listener)
      Sets the listener on the click events of this button.
      void setPressed​(boolean pressed)
      Changes the clickable element state.
      • Methods inherited from class ej.mwt.Widget

        addClassSelector, contains, containsWidget, getAbsoluteX, getAbsoluteY, getContentBounds, getDesktop, getHeight, getParent, getStyle, getWidgetAt, getWidth, getX, getY, hasClassSelector, isAttached, isEnabled, isShown, isTransparent, onAttached, onDetached, onHidden, onLaidOut, onShown, removeAllClassSelectors, removeClassSelector, render, requestLayOut, requestRender, requestRender, setClassSelectors, setEnabled, setPosition, setStyle, updateStyle
      • Methods inherited from class java.lang.Object

        clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Button

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

        public Button​(java.lang.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)
        Overrides:
        isInState in class ej.mwt.Widget
      • handleEvent

        public boolean handleEvent​(int event)
        Overrides:
        handleEvent in class ej.mwt.Widget
      • 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.
      • handleClick

        @Deprecated
        public void handleClick()
        Deprecated.
        Internal API.
        Handles a click event.