While some button types, such as radio buttons and checkboxes, are always toggle buttons, other types, including push buttons, may optionally be configured to behave as toggle buttons. For example, see the push buttons in the application below. Clicking a button causes it to retain a pressed state; clicking it again releases the button:
The BXML source for the example is below:
<Window title="Toggle Buttons" maximized="true" xmlns:bxml="http://pivot.apache.org/bxml" xmlns:content="org.apache.pivot.wtk.content" xmlns="org.apache.pivot.wtk"> <BoxPane styles="{padding:4, horizontalAlignment:'center', verticalAlignment:'center'}"> <PushButton toggleButton="true"> <content:ButtonData text="Anchor" icon="/org/apache/pivot/tutorials/anchor.png"/> </PushButton> <PushButton toggleButton="true"> <content:ButtonData text="Cup" icon="/org/apache/pivot/tutorials/cup.png"/> </PushButton> <PushButton toggleButton="true"> <content:ButtonData text="Star" icon="/org/apache/pivot/tutorials/star.png"/> </PushButton> </BoxPane> </Window>
Note that the push buttons in the example display both an icon and a text label. The data for each button is specified as an instance of org.apache.pivot.wtk.content.ButtonData, which defines "icon" and "text" properties. In addition to simple string data, the default button data renderer is also capable of displaying button data provided in this manner.
Also note that the values of the "icon" attributes begin with a slash. This represents a location relative to the application's classpath. If the file name began with an '@' symbol instead, the value would represent a location relative to the BXML file currently being loaded.
Finally, note that the buttons' states are all managed independently: clicking one button does not affect the selection state of the others. This is because the buttons are not part of a group. Had the buttons all been assigned to the same group, only a single button would be selected at a time, and clicking one button would automatically deselect the previously selected button.
Because this application doesn't require any special logic, there is no Java source for this example; the example is launched using the org.apache.pivot.wtk.ScriptApplication class.
Next: Radio Buttons