Adding a button component

VisualAge provides button beans from the com.sun.java.swing and java.awt packages.

You can add a button bean to enable the user to perform an action or select a state.


Bean Description
JButton (Swing) or Button (AWT) A push button, generally used to perform a function
JCheckBox (Swing) or Checkbox (AWT) A setting button that is checked when selected
JRadioButton (Swing) or CheckboxGroup (AWT) A radio button or group for mutually exclusive settings
JToggleButton (Swing) A two-state push button that appears to be pushed in when selected

Adding or customizing graphic images for Swing buttons

You can add or replace images for Swing buttons, such as JButtons and JCheckBoxes. Specify the graphic file for one or more image properties in the component's property sheet.
Property Represents...
icon unselected button
pressedIcon pressed button
selectedIcon selected button
disabledIcon unselected disabled button
disabledSelectedIcon selected disabled button
rolloverIcon unselected rollover button
rolloverSelectedIcon selected rollover button

Defining the initial state of a toggle component

Set the selection or state property in the component's property sheet. For initial selection, set the property value to True. Otherwise, the value should be False. If the component is one of a group of mutually exclusive choices, only one member of the group should be initially selected.

If applicable, assign a toggle component to a group of mutually exclusive choices. Only one member of the group can be selected at a time.

Adding a group of JToggleButton or JRadioButton beans

Use a ButtonGroup to define a group of buttons:

  1. Add the buttons for the group.
  2. Add a ButtonGroup bean to the free-form surface.
  3. Save the composite bean.
  4. On the Methods page, add user code in the get method of each button to add the button to the button group. For example, to add JRadioButton1 to ButtonGroup1, add this code to the getJRadioButton1() method:
    getButtonGroup1.add(ivjJRadioButton1);
    

Adding a group of Checkboxes as radio buttons

Use a CheckboxGroup bean to define a group of radio buttons:

  1. Add the check boxes for the group.

  2. Add a CheckboxGroup bean to the free-form surface.

  3. In the property sheet of each Checkbox bean, specify the get method of the CheckboxGroup for the checkboxGroup property. For example, to add Checkbox to CheckboxGroup1, specify getCheckboxGroup1() for the checkboxGroup property.

Calling a method when a button is selected

Connect the button's actionPerformed(java.awt.event.ActionEvent) event to the method on the target bean.

Obtaining the selected choice from a group

From the group's popup menu, tear off the property that represents the selected choice. For a ButtonGroup, tear off the selection property. For a CheckboxGroup, tear off the selectedCheckbox property. After tearing off the property, you can make connections to features of the Variable that represents the selected choice property.

Disabling a button

Connect a related event to the button's enabled property and set the connection parameter value to False.

Enabling a button

Connect a related event to the button's enabled property and set the connection parameter value to True.

For examples, see the ToDoList class in the com.ibm.ivj.examples.vc.todolist package, and the JRadioButtonPanel and RadioButtonPanel classes in the com.ibm.ivj.examples.vc.utilitybeans package. These examples are shipped in the IBM Java Examples project.


Related procedures
Visual bean basics
Tearing off properties
Using VisualAge beans in visual composition
Adding the IBM Java Examples project

Related references
Button beans