Package ej.widget.basic
Class Button
- java.lang.Object
-
- ej.mwt.Widget
-
- ej.widget.basic.Label
-
- ej.widget.basic.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");
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"));
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));
- Since:
- 2.3.0
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class ej.mwt.Widget
Widget.WidgetListener
-
-
Field Summary
-
Fields inherited from class ej.mwt.Widget
DEBUG_WIDGET_ENABLED_CONSTANT, DEBUG_WIDGET_MONITOR_CONSTANT, NO_CONSTRAINT
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanhandleEvent(int event)Handles the given event.booleanisInState(int state)Gets whether or not the widget is in the given state.voidsetOnClickListener(OnClickListener listener)Sets the listener on the click events of this button.voidsetPressed(boolean pressed)Changes the clickable element state.-
Methods inherited from class ej.widget.basic.Label
computeContentOptimalSize, getText, renderContent, setText
-
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, setX, setY, updateStyle
-
-
-
-
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:WidgetGets whether or not the widget is in the given state.
-
handleEvent
public boolean handleEvent(int event)
Description copied from class:WidgetHandles the given event. Does nothing by default and returnsfalse(does not consume event).Called by the desktop event manager.
- Overrides:
handleEventin classWidget- Parameters:
event- the event to handle.- Returns:
trueif the widget has consumed the event,falseotherwise.
-
setPressed
public void setPressed(boolean pressed)
Description copied from interface:ClickableChanges the clickable element state.- Specified by:
setPressedin interfaceClickable- Parameters:
pressed-trueif the element is pressed,falseotherwise.
-
-