Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 261
Registered: ‎08-11-2009
My Device: Not Specified

checkbox group help needed

Hi ,

 

I have 2 checkbox  

1. income

2. expense

 

i want to select only one at a time . can any tell me how this can be implemented in blackberry 4.5 , does any checkbox group exsit for blackberry .

 

willbe thankfull if any one can help me to solve this issue

 

Thanks

SujithRavindran

 

 

 

Developer
Posts: 213
Registered: ‎07-18-2008
My Device: Not Specified

Re: checkbox group help needed

use FieldChangeListener. This example shows how to use it  but not on checkboxes:

 

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800505/800608/...

--------------------------------------------------
problemSolved() ? kudosPlease():kudosPlease();
Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: checkbox group help needed

A grouping class exists only for the ChoiceField, not for the Checkbox. It should not be difficult to implement it yourself though.

If you don't want to listen to the immediate change of the checkbox you can implement the control as a FieldChangeListener, to set itself to each field that is added. In the fieldChanged method just iterate through all added fields and reverse the checked status.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: checkbox group help needed

If it is of only two checkbox field.. give a try like this.. if it is more fields better to go for Simon's approach.

 

 

        final CheckboxField cbf = new CheckboxField("Cricket",false);
        final CheckboxField cbf1 = new CheckboxField("Baseball",false);
        add(cbf);
        add(cbf1);
        
        cbf.setChangeListener(new FieldChangeListener ()
        {
            public void fieldChanged(Field field, int context) 
            {
                if(cbf.getChecked() == true && cbf1.getChecked() == true)
                {
                    cbf1.setChecked(false);
                }
            }
        });
        
        cbf1.setChangeListener(new FieldChangeListener ()
        {
            public void fieldChanged(Field field, int context) 
            {
                if(cbf.getChecked() == true && cbf1.getChecked() == true)
                {
                    cbf.setChecked(false);
                }
            }
        });

 

 


Use Search. "Accept Solution" If the problem is resolved.
Highlighted
New Developer
Posts: 11
Registered: ‎08-07-2008
My Device: Not Specified

Re: checkbox group help needed

class CheckboxFieldChangeListener extends FieldChangeListener
{
    private CheckboxField[] m_arrFields;
   
    public CheckboxFieldChangeListener (CheckboxField[] arrFields)
    {
        m_arrFields = arrFields;
    }
   
    public void fieldChanged(Field field, int context)
    {
        if(true == field.getChecked())
        {
            for(int i = 0; i < m_arrFields.length; i++)
            {
                CheckboxField oField = m_arrFields[i];
                if((oField != field) && (oField.getChecked()))
                {
                    oField.setChecked(false);
                }
            }
        }
    }   
}

 

// Create checkboxes

final CheckboxField oIncomeCheckboxField = new CheckboxField("Income", false);
final CheckboxField oExpenseCheckboxField = new CheckboxField("Expense", false);


//Add to the screen

add(oIncomeCheckboxField);
add(oExpenseCheckboxField);

 

// Create listener

CheckboxFieldChangeListener  oCheckboxFieldChangeListener  = new CheckboxFieldChangeListener();

 

// Set listener

oIncomeCheckboxField.setChangeListener(oCheckboxFieldChangeListener );
oExpenseCheckboxField.setChangeListener(oCheckboxFieldChangeListener );

 

 

You can use vector instead of array.

 

eDip

Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: checkbox group help needed

the CheckboxFieldChangeListener can set itself as a listener to each added field. using a vector allows to add fields dynamically.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter