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 WTKX source for the example is below:
<Window title="Toggle Buttons" maximized="true" xmlns:wtkx="http://pivot.apache.org/wtkx" xmlns:content="org.apache.pivot.wtk.content" xmlns="org.apache.pivot.wtk"> <content> <BoxPane styles="{padding:4, horizontalAlignment:'center', verticalAlignment:'center'}"> <PushButton toggleButton="true"> <buttonData> <content:ButtonData text="Anchor" icon="org/apache/pivot/tutorials/anchor.png"/> </buttonData> </PushButton> <PushButton toggleButton="true"> <buttonData> <content:ButtonData text="Cup" icon="org/apache/pivot/tutorials/cup.png"/> </buttonData> </PushButton> <PushButton toggleButton="true"> <buttonData> <content:ButtonData text="Star" icon="org/apache/pivot/tutorials/star.png"/> </buttonData> </PushButton> </BoxPane> </content> </Window>
The Java source is as follows. It simply loads the WTKX source and displays the window; there's no additional logic involved:
package org.apache.pivot.tutorials.buttons; import org.apache.pivot.collections.Map; import org.apache.pivot.wtk.Application; import org.apache.pivot.wtk.DesktopApplicationContext; import org.apache.pivot.wtk.Display; import org.apache.pivot.wtk.Window; import org.apache.pivot.wtkx.WTKXSerializer; public class ToggleButtons implements Application { private Window window = null; public void startup(Display display, Map<String, String> properties) throws Exception { WTKXSerializer wtkxSerializer = new WTKXSerializer(); window = (Window)wtkxSerializer.readObject(this, "toggle_buttons.wtkx"); window.open(display); } public boolean shutdown(boolean optional) { if (window != null) { window.close(); } return false; } public void suspend() { } public void resume() { } public static void main(String[] args) { DesktopApplicationContext.main(ToggleButtons.class, args); } }
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 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.
Note also that the values of the "icon" attributes do not begin with an '@' symbol. In WTKX, image URLs are commonly specified with this prefix, to indicate that an image's location is relative to the WTKX source file currently being loaded. An image path without an '@' prefix, such as those in this example, is generally treated as an absolute location relative to the application's classpath.
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.
Next: Radio Buttons