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
Developer
TEP
Posts: 42
Registered: ‎02-17-2009
My Device: Not Specified
Accepted Solution

RadioButton Listener

[ Edited ]

I am a beginning programmer and have been trying to resolve my question in the quest of learning. However, I have spent quite a bit of time on this one area and was wondering of someone could help point me in the right direction.

 

To set the stage, I have two RadioButtonFields in a RadioButtonGroup. The RadioButtonGroup is set within a Layout manager so that I can display the rbf's on the screen where I want them. Anyway, I have coded some functions for the first button or index 0. I have not coded anything for the second button or index 1. I am attempting to have index 1 display a dialog.alert and then reset the choices so that a user could go back and select index 0 for the time being.

 

When index 1 is selected, the dialog.alert displays but when "ok" is selected and I go to select index 0,  I get a JVM Error 104, IllegalStateException. Can someone look at what I have coded and help me implement what I am trying to do the proper way? It will be most appreciated.

 

 

RadioButtonLayout rblType = new RadioButtonLayout();
final RadioButtonGroup rbgType = new RadioButtonGroup();

RadioButtonField rbfOne = new RadioButtonField(" One", rbgType, false);
rblType.add(rbfOne);

RadioButtonField rbfTwo = new RadioButtonField(" Two", rbgType, false);
rblType.add(rbfTwo);
vfmMain.add(rblType);

rbgType.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
if (rbgType.getSelectedIndex() == 0) {
add(vfmBody1);
}
if (rbgType.getSelectedIndex() == 1) {
if (vfmBody1.isVisible() == true) {
delete(vfmBody1);

if (vfmBody2.isVisible() == true) {
delete(vfmBody2);
}
}
Dialog.alert("Feature unavailable at this time. This will be implemented in a " +
"future release.");

rbgType.setChangeListener(null);
rbgType.setSelectedIndex(0);
}
}
});

 

 

 

 

Message Edited by TEP on 02-23-2009 08:50 PM
BlackBerry Development Advisor
MSohm
Posts: 14,757
Registered: ‎07-09-2008
My Device: BlackBerry Passport

Re: RadioButton Listener

You'll need to make the screen update in a block described here:

 

How to - Update a screen on the Main Event Thread
Article Number: DB-00136

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

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Developer
TEP
Posts: 42
Registered: ‎02-17-2009
My Device: Not Specified

Re: RadioButton Listener

Thank you for helping me. I have tried using the method described in the Article given and I am not having any success. I have spent some more time looking at the labs from RIM and looking through the forums. It is my understanding that I would use UiApplication.getApplication() if I were pulling or pushing from another screen in an application that has more than one screen implemented. Please correct me if my understanding is incorrect.

 

I am currently working with one class (screen) at the moment that is extending MainScreen. I have the different sections of GUI components placed into separate field managers. I have implemented two buttons in addition to the RadioButtons that I am having difficulty with. I have also implemented a listener for each button that is able to add and delete the separate field managers without any issues.

 

Since I am keeping everything in one screen for the time being do I need to just use RadioButtonFields by themselves rather than implementing the RadioButtonFields into a RadioButtonGroup? I could then add a listener for each RadioButtonField to do something similar with what I am doing with the ButtonFields. From what I have read though, I thought I was supposed to implement RadioButtonFields into a RadioButtonGroup.

 

I do appreciate your help. I suppose it is not efficient to place the components into field managers and the proper way should be to make separate screens. Although looking at some of the examples from RIM,they are contained in one screen as well.

Developer
BBDeveloper
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: RadioButton Listener

Hi, I did try your code and it worked, when you select the index 1 the dialog comes when you press ok the selection goes to 0 and the listener will not respond anymore as you have set teh listener to null.

Use Search. "Accept Solution" If the problem is resolved.
Developer
Developer
TEP
Posts: 42
Registered: ‎02-17-2009
My Device: Not Specified

Re: RadioButton Listener

[ Edited ]

I thought I had to set the currently selected index (or RadioButton) to null so that the FieldChangeListener will once again listen for events on the two RadioButtons. I have changed the code as follows:

 

 

RadioButtonLayout rblType = new RadioButtonLayout();
final RadioButtonGroup rbgType = new RadioButtonGroup();

RadioButtonField rbfOne = new RadioButtonField(" One", rbgType, false);
rblType.add(rbfOne);

RadioButtonField rbfTwo = new RadioButtonField(" Two", rbgType, false);
rblType.add(rbfTwo);
vfmMain.add(rblType);

rbgType.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
if (rbgType.getSelectedIndex() == 0) {
add(vfmBody1);
}
if (rbgType.getSelectedIndex() == 1) {
if (vfmBody1.isVisible() == true) {
delete(vfmBody1);

if (vfmBody2.isVisible() == true) {
delete(vfmBody2);
}
}
Dialog.alert("Feature unavailable at this time. This will be implemented in a " +
"future release.");
}
}
});

 

The reason I thought I had to have the listener set to null is because whenever I select the first RadioButton the code is executed. I can then select the second RadioButton and that code is executed. When I click "OK" to the dialog popup all is well until I select the first RadioButton again. When I select the first RadioButton a second time, I get a Java IllegalStateException. If I start over after resetting the simulator and select the second RadioButton first, respond to the dialog box, and then try to select the first RadioButton, I get the same IllegalStateException.

 

I am definately learning here and appreciate your input as well. What am I overlooking? Do you think it is the dialog box that is creating my problem?

 

Message Edited by TEP on 02-27-2009 09:01 AM
Developer
Developer
TEP
Posts: 42
Registered: ‎02-17-2009
My Device: Not Specified

Re: RadioButton Listener

I just wanted to provide an update to the post that I started. The code for RadioButtons that I put together indeed did work, however, there was something about deleting field managers that kept blowing things up for me. I managed to get my application into debug mode and although I still don't understand how to use that fully, I did manage to get another message stating that the application didn't have permission to delete the component I was trying to remove

 

In the end, I took MSohm's input and created separate screens for my components and field managers to be placed on. I then implemented pushing them onto the current screen depending on which radio button was pushed.