The Button is the primary user interface
component that is normally used for finalizing input and initiating
some action. The link style allows it to look
like a hyperlink. When the user clicks a button, a
Button.ClickEvent is emitted. A listener that
inherits the Button.ClickListener interface
can handle clicks with the buttonClick() method.
public class TheButton extends CustomComponent implements Button.ClickListener {
Button thebutton;
public TheButton() {
/* Create a Button with the given caption. */
thebutton = new Button ("Do not push this button");
/* Listen for ClickEvents. */
thebutton.addListener(this);
setCompositionRoot(thebutton);
}
/** Handle button click events from the button. */
public void buttonClick (Button.ClickEvent event) {
thebutton.setCaption ("Do not push this button again");
}
}
As a user interface often has several buttons, you can differentiate
between them either by comparing the Button
object reference returned by getButton()
method of Button.ClickEvent to a kept
reference or by using a separate listener method for each button. The
listening object and method can be given to the constructor. For a
detailed description of these patterns together with some examples,
please see the section called “Events and Listeners”.
With setSwitchMode(true), the
Button will look like a check box, a
two-mode selection component. The caption of the check box will be
placed on the right side of the check box.
Clicking on the check box will change the state. The state is the
Boolean property of the
Button, and can be obtained with
getValue() method of the
Button. For an example about the use of
checkbox in a table, see the section called “Table”.
A Button is not just a button, but with
the link style it can look like a
hyperlink. This is useful in many situations where a full-blown
button would be just too much, or where you want to allow user to
open some resource that is typically opened as a
hyperlink. Notice that the link is nevertheless not hyperlink, as
it is normally not bound to a URI. The functionality of the
Button stays the same despite the look,
and click on the link emits a
Button.ClickEvent just like for normal
buttons.
See also the Link component in the section called “Link”.