Menu buttons are similar to list buttons, but provide access to "drop-down" menus rather than list views. They are often used in toolbars, but can be placed anywhere within an application's user interface.
The following sample application demonstrates the use of a menu button. Selecting an option from the menu button on the left adds the corresponding component type to the content area on the right:
The BXML source for the application is as follows:
<menus:MenuButtons title="Menu Buttons" maximized="true" xmlns:bxml="http://pivot.apache.org/bxml" xmlns:content="org.apache.pivot.wtk.content" xmlns:menus="org.apache.pivot.tutorials.menus" xmlns="org.apache.pivot.wtk"> <Border styles="{padding:8}"> <TablePane styles="{horizontalSpacing:4}"> <columns> <TablePane.Column width="-1"/> <TablePane.Column width="1*"/> </columns> <TablePane.Row height="1*"> <BoxPane orientation="vertical"> <MenuButton buttonData="Add Component"> <Menu> <Menu.Section> <Menu.Item buttonData="Push Button" action="addPushButton"/> <Menu.Item buttonData="Checkbox" action="addCheckbox"/> <Menu.Item buttonData="Radio Button" action="addRadioButton"/> </Menu.Section> </Menu> </MenuButton> </BoxPane> <Border> <ScrollPane> <BoxPane bxml:id="componentBoxPane" orientation="vertical" styles="{padding:4, spacing:4}"/> </ScrollPane> </Border> </TablePane.Row> </TablePane> </Border> </menus:MenuButtons>
Like the previous example, it defines a set of menu items that are associated with named actions. The Java source, which defines the actions, is shown below:
package org.apache.pivot.tutorials.menus; import java.net.URL; import org.apache.pivot.beans.Bindable; import org.apache.pivot.collections.Map; import org.apache.pivot.util.Resources; import org.apache.pivot.wtk.Action; import org.apache.pivot.wtk.BoxPane; import org.apache.pivot.wtk.Checkbox; import org.apache.pivot.wtk.Component; import org.apache.pivot.wtk.PushButton; import org.apache.pivot.wtk.RadioButton; import org.apache.pivot.wtk.Window; public class MenuButtons extends Window implements Bindable { private BoxPane componentBoxPane = null; public MenuButtons() { Action.getNamedActions().put("addPushButton", new Action() { @Override public void perform(Component source) { componentBoxPane.add(new PushButton("Push button")); } }); Action.getNamedActions().put("addCheckbox", new Action() { @Override public void perform(Component source) { componentBoxPane.add(new Checkbox("Checkbox")); } }); Action.getNamedActions().put("addRadioButton", new Action() { @Override public void perform(Component source) { componentBoxPane.add(new RadioButton("Radio button")); } }); } @Override public void initialize(Map<String, Object> namespace, URL location, Resources resources) { componentBoxPane = (BoxPane)namespace.get("componentBoxPane"); } }
Next: Color Choosers