Creating a button field set
A button field set is a set of buttons that all have the same width on the screen. You can use the VerticalButtonFieldSet class to align buttons vertically on a screen, and you can use the HorizontalButtonFieldSet class to align buttons horizontally on a screen. The width of the buttons can change depending on properties such as the width of the screen or the content of the buttons. By default, the buttons in a VerticalButtonFieldSet expand to the width of the largest button in the set, but you can alter this behavior by using style bits. For example, you can specify the USE_ALL_WIDTH style bit of the Field class when you create a VerticalButtonFieldSet object to direct the buttons to use the entire available width of the screen. The buttons in a HorizontalButtonFieldSet are placed in a horizontal row that fills the entire width of the screen, and the buttons are resized automatically to have equal widths.

You might use a VerticalButtonFieldSet object to make sure that a set of buttons all have the same size regardless of the content of the buttons. You might use a HorizontalButtonFieldSet object to place buttons along the top or bottom of a screen, such as confirmation or cancel buttons.
Create a button field set
- Import the required classes and interfaces.
import com.blackberry.toolkit.ui.container.HorizontalButtonFieldSet;
import com.blackberry.toolkit.ui.container.VerticalButtonFieldSet;
import net.rim.device.api.ui.component.ButtonField;
- To create a vertical button field set, do the following:
- Create a new VerticalButtonFieldSet object to contain the buttons.
VerticalButtonFieldSet setOne = new VerticalButtonFieldSet();
- Create new ButtonField objects to add to the VerticalButtonFieldSet. To set the margins (the space around each of the buttons in the set) of the buttons, invoke setMargin() for each ButtonField object.
ButtonField pieButton = new ButtonField( "Pie" );
ButtonField cakeButton = new ButtonField( "Cake" );
ButtonField chocolateButton = new ButtonField( "Chocolate" );
ButtonField cookiesButton = new ButtonField( "Cookies" );
ButtonField fruitButton = new ButtonField( "Fruit" );
pieButton.setMargin( 5, 5, 5, 5 );
cakeButton.setMargin( 5, 5, 5, 5 );
chocolateButton.setMargin( 5, 5, 5, 5 );
cookiesButton.setMargin( 5, 5, 5, 5 );
fruitButton.setMargin( 5, 5, 5, 5 );
- Add the ButtonField objects to the VerticalButtonFieldSet.
setTwo.add( pieButton );
setTwo.add( cakeButton );
setTwo.add( chocolateButton );
setTwo.add( cookiesButton );
setTwo.add( fruitButton );
- Add the VerticalButtonFieldSet object to the screen.
add( setOne );
- To create a horizontal button field set, do the following:
- Create a new HorizontalButtonFieldSet object to contain the buttons.
HorizontalButtonFieldSet setTwo = new HorizontalButtonFieldSet();
- Create new ButtonField objects to add to the HorizontalButtonFieldSet. To set the margins of the buttons, invoke setMargin() for each ButtonField object.
ButtonField saveButton = new ButtonField( "Save" );
ButtonField discardButton = new ButtonField( "Discard" );
ButtonField cancelButton = new ButtonField( "Cancel" );
saveButton.setMargin( 5, 5, 5, 5 );
discardButton.setMargin( 5, 5, 5, 5 );
cancelButton.setMargin( 5, 5, 5, 5 );
- Add the ButtonField objects to the HorizontalButtonFieldSet.
setTwo.add( saveButton );
setTwo.add( discardButton );
setTwo.add( cancelButton );
- Add the HorizontalButtonFieldSet object to the screen.
add( setTwo );
Was this information helpful? Send us your comments.