public class Button extends Label
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(States.ACTIVE))); pressedButtonStyle.setBackground(new RectangularBackground(Colors.GRAY));
Modifier and Type | Field and Description |
---|---|
static int |
ACTIVE
Active state.
|
NO_CONSTRAINT
Constructor and Description |
---|
Button()
Creates a button with an empty text.
|
Button(String text)
Creates a button with the given text to display.
|
Modifier and Type | Method and Description |
---|---|
void |
handleClick()
Handles a click event.
|
boolean |
handleEvent(int event)
Handles the given event.
|
boolean |
isInState(int state)
Gets whether or not the widget is in the given state.
|
void |
setOnClickListener(OnClickListener listener)
Sets the listener on the click events of this button.
|
computeContentOptimalSize, getText, renderContent, setText
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
public static final int ACTIVE
public Button()
public Button(String text)
text
- the text to display.public void handleClick()
public boolean handleEvent(int event)
Widget
false
(does not consume event).
Called by the desktop event manager.
handleEvent
in class Widget
event
- the event to handle.true
if the widget has consumed the event, false
otherwise.public boolean isInState(int state)
Widget
public void setOnClickListener(@Nullable OnClickListener listener)
listener
- the listener to set.