www.ibm.comLANDP Webpage

Useful Tips - Passing constants as parameters

There are several methods in the ldpbeans.jar package that require a parameter that can only take a certain value. I.e. The getRecord(int searchType) method for the SharedFile bean requires a parameter. This integer value can be one of two constants defined in the LandpSHFILEConst interface:

As theses are the only two values allowable for this parameter, we can construct a radio button group that restricts the users input.

Take a JRadioButton from the Swing category of the component palette and place it into your Jframe. Repeat the process so you have two JRadioButtons in your Jframe. Change the text property of the JRadioButton1 to 'Next' and the beanName to 'nextButton'. Similarly change the text property of the JRadioButton2 to 'Previous' and the beanName to 'previousButton'. We now need to add a JButtonGroup to our workspace so that we can mange the button group.

Click the 'Choose bean...' icon and add a 'javax.swing.JButtonGroup' object called 'ButtonGroup' to your workspace. The purpose of the button group is to ensure that we can only ever have one of the two buttons selected at any one time. To add our buttons to the ButtonGroup , we need to call the add(button b) method of the ButtonGroup object. We shall call this method when the application window is opened.

Connect the 'windowOpened' event of the JFrame to the 'add(AbstractButton b)' method of the ButtonGroup object.

Add the 'Previous' JRadioButton to the Buttongroup. Use step 3 as a Guideline. Now when you run the application, selecting one JRadioButton will result in the other becoming deselected. How do we get an integer parameter from two JRadioButtons?. The answer is to write a small method that determines which button is selected and then returns the appropriate constant value. Thus for our application, the method below would suffice.

/**
    Determine which search mode is required and return the
    appropriate integer value.
*/
public int searchType() {
    int searchMode = 0;
    if( getnextButton().isSelected() )
        searchMode = LANDP_S_SHFILE_RECORD_NEXT;
    if( getpreviousButton().isSelected() )
        searchMode = LANDP_S_SHFILE_RECORD_PREVIOUS;
    return searchMode;
}

To connect this method to the dashed connection, we must perform the following steps.

Now whenever you click 'Get Record', the correct integer value will always be passed as a parameter.

Introduction New to LANDP? Make the most of LANDP Solving LANDP problems Feedback