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"); EditableStyle buttonStyle = new EditableStyle(); buttonStyle.setBackgroundColor(Colors.SILVER); buttonStyle.setBorder(new SimpleRectangularBorder(1)); buttonStyle.setFontProfile(new FontProfile("", 23, Font.STYLE_PLAIN)); button.mergeStyle(buttonStyle);
This example shows a styled button with its background color changing on a press action:
Button button = new Button("Press Me"); Stylesheet stylesheet = StyleHelper.getStylesheet(); EditableStyle buttonStyle = new EditableStyle(); buttonStyle.setBackgroundColor(Colors.SILVER); buttonStyle.setBorder(new SimpleRectangularBorder(1)); buttonStyle.setFontProfile(new FontProfile("", 23, Font.STYLE_PLAIN)); stylesheet.addRule(new TypeSelector(Button.class), buttonStyle); EditableStyle pressedButtonStyle = new EditableStyle(); pressedButtonStyle.setBackgroundColor(Colors.GRAY); stylesheet.addRule(new AndCombinator(new TypeSelector(Button.class), new StateSelector(State.Active)), pressedButtonStyle);
EMPTY_ELEMENT_ARRAY
Constructor and Description |
---|
Button()
Creates a button with an empty text.
|
Button(java.lang.String text)
Creates a button with the given text to display.
|
Modifier and Type | Method and Description |
---|---|
void |
addOnClickListener(OnClickListener listener)
Adds a listener on the click events of the button.
|
boolean |
handleEvent(int event)
Called by the system if the widget is the owner of the focus of the active panel.
|
boolean |
isInState(State state)
Gets whether or not the element is in the given state.
|
protected void |
onClick()
Invoked when the button has been clicked.
|
void |
performClick()
Performs the actions associated to a click.
|
void |
removeOnClickListener(OnClickListener listener)
Removes a listener on the click events of the button.
|
getText, renderContent, setText, updateText, validateContent
addClassSelector, contains, gainFocus, getAttribute, getChild, getChildrenCount, getChildrenElements, getContentBounds, getParentElement, getStyle, hasClassSelector, isTransparent, lostFocus, mergeStyle, removeAllClassSelectors, removeClassSelector, removeInstanceStyle, render, setClassSelectors, setEnabled, updateStyle, updateStyleOnly, validate
getAbsoluteX, getAbsoluteX, getAbsoluteY, getAbsoluteY, getEventHandler, getHeight, getPanel, getParent, getPreferredHeight, getPreferredWidth, getRelativeX, getRelativeY, getWidgetAt, getWidth, getX, getY, hasFocus, hideNotify, invalidate, isEnabled, isShown, isValid, isVisible, repaint, repaint, requestFocus, requestFocus, revalidate, revalidateSubTree, setBounds, setEventHandler, setLocation, setPreferredSize, setSize, setVisible, showNotify
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getEventHandler, getHeight, getWidth, getX, getY, hideNotify, isShown, repaint, repaint, setEventHandler, showNotify
public Button()
public Button(java.lang.String text)
null
.text
- the text to display.java.lang.NullPointerException
- if the text is null
.protected void onClick()
public void performClick()
public void addOnClickListener(OnClickListener listener)
listener
- the listener to add.java.lang.NullPointerException
- if the given listener is null
.public void removeOnClickListener(OnClickListener listener)
listener
- the listener to remove.public boolean isInState(State state)
Element
isInState
in interface Element
isInState
in class StyledWidget
state
- the state to check.true
if the element is in the given state, false
otherwise.public boolean handleEvent(int event)
Widget
If an event handler is registered, its EventHandler#handleEvent(int)
method is called.
Otherwise, do nothing and return false
(do not consume event).
handleEvent
in interface Renderable
handleEvent
in class Widget
event
- the event to handle.true
if the widget has consumed the event, false
otherwise.Widget.setEventHandler(EventHandler)