Strange radio button behaviour

86 views
Skip to first unread message

Ekrem Saban

unread,
Aug 27, 2014, 10:37:43 AM8/27/14
to codenameone...@googlegroups.com
If you are experiencing an issue please mention the full platform your issue applies to:
IDE: Eclipse Lumia
Desktop OS: Windows 8.1
Simulator: Codename One simulator
Device:still testing!
Hello!

I am about to write my first app using Codename One. Perhaps, I think that I have overseen some information about radio buttons, as my first MathsApp is not working properly.

Here is the code section with the problem:

/**
 * Your application code goes here
 */
package userclasses;

[…]
/**
 * State machine for the maths app
 * 
 * @author Ahmet Ekrem Saban
 * @since 2014-08-15
 */
public class StateMachine extends StateMachineBase {
/**
* The field that says to calculate the combination of 
* the string characters.
*/
private boolean combine;
/** The button group for the two radio buttons. */
private ButtonGroup radioButtons;

[…]
/**
* Decides whether to calculate the combination or permutation of the
* input string.
*/
private void determineRadioButtonState() {
final RadioButton combineRadioButton = findCombineRadioButton();
final RadioButton permutateRadioButton = findPermutateRadioButton();

if (radioButtons != null && radioButtons.getButtonCount() == 0) {
radioButtons.add(combineRadioButton);
radioButtons.add(permutateRadioButton);
printState(combineRadioButton, permutateRadioButton);
combineRadioButton.pressed();
printState(combineRadioButton, permutateRadioButton);
permutateRadioButton.pressed();
printState(combineRadioButton, permutateRadioButton);
}
final boolean isCombination = combineRadioButton.isSelected();
final boolean isPermutation = permutateRadioButton.isSelected();
combine = (isCombination || !isPermutation) ? false : true;
}
/**
* Prints the state of the given buttons
* @param radioButtons
*/
private void printState(final RadioButton... radioButtons) {
if (radioButtons != null) {
for (final RadioButton radioButton : radioButtons) {
System.out.println("printState"  + "combineRadioButton is "
+ ((radioButton.isSelected()) ? "" : "not ") + "pressed.");
}
}
}

/**
* This method should be used to initialise variables instead of
* the constructor/class scope to avoid race conditions
* @param resource
*/
@Override
protected void initVars(final Resources resource) {
combine = true;
radioButtons = new ButtonGroup();
}

[…]
}


I expect that the combine radio button is set first, then set by the pressed() method and then deselected, as the permutate button is pressed. But the result diverts from my expectations:

printStatecombineRadioButton is pressed.
printStatecombineRadioButton is not pressed.
printStatecombineRadioButton is pressed.
printStatecombineRadioButton is not pressed.
printStatecombineRadioButton is pressed.
printStatecombineRadioButton is not pressed.

Where am I making the error?

Thank you in advance!

Ekrem Saban

unread,
Aug 27, 2014, 10:55:05 AM8/27/14
to codenameone...@googlegroups.com
If you are experiencing an issue please mention the full platform your issue applies to:
IDE: Eclipse Lumia
Desktop OS: Windows 8.1
Simulator: CN simulator
Device: still testing!
Hello!

Originally, I added two radio buttons to my design with a button group that contains them. But if you take the yellow splotches in the image below as clicks, the radio buttons "take" some time to realise that they are indeed radio buttons.

This is just for making the question above easier to understand.

Shai Almog

unread,
Aug 27, 2014, 1:03:42 PM8/27/14
to codenameone...@googlegroups.com
Use setSelected(true) not pressed.

Ekrem Saban

unread,
Aug 28, 2014, 2:31:14 AM8/28/14
to codenameone...@googlegroups.com
Thank you for your reply. That worked, but when I first press the radio button automatically by this method, the appearance remains unpressed. This kind of to me unexpected behaviour is also true for [Clear] & [Enter] buttons that are activated by the app only after a text has been added; I expect some major difference in the button's appearance between the states "active" and "inactive". See the image below...


Best regards!

On Wednesday, August 27, 2014 7:03:42 PM UTC+2, Shai Almog wrote:
Use setSelected(true) not pressed.

Ekrem Saban

unread,
Aug 28, 2014, 2:37:58 AM8/28/14
to codenameone...@googlegroups.com
I think I now where the problem I have is: I cannot refresh the app appropriately. What I need is to refresh the whole "Main" frame. There are also other unexpected behaviours when I enter a text and press the [Enter] button, as the result in the list below the buttons is not immediately visible. How can I call a refresh on the "Main" without going through the whole elements and refresh them one by one?

Chen Fishbein

unread,
Aug 28, 2014, 7:26:43 AM8/28/14
to codenameone...@googlegroups.com
Hi,
To be honest, It is still not obvious from the diagram what you are doing and what you expect to happen, but each component has a repaint() method which you can invoke if you want the whole screen to redraw call repaint on your Form.

Shai Almog

unread,
Aug 28, 2014, 9:49:11 AM8/28/14
to codenameone...@googlegroups.com
press() presses down the button as if the user physically is pressing it down. Without a call to release() this is the equivalent of a user holding down both buttons.
This method is designed for apps that want to simulate user interaction.

Ekrem Saban

unread,
Aug 28, 2014, 10:02:57 AM8/28/14
to codenameone...@googlegroups.com
Thank you for your reply! Now, I managed my problem at last with the "unallowed" code

public class StateMachine extends StateMachineBase {

// VARIABLES
// --------------------------------
/** Whether the radio buttons are ready or not */
private boolean radioButtonsArePrepared = false;

// CONSTRUCTORS
// --------------------------------
public StateMachine(final String resFile) {
super(resFile);
prepareRadioButtons();
// Do not modify, write code in initVars and initialise class members there.
// The constructor might be invoked too late due to race conditions that might occur.
}

// METHODS
// --------------------------------

private void prepareRadioButtons() {
if (!radioButtonsArePrepared) {
final RadioButton combineRadioButton = findCombineRadioButton();
final RadioButton permutateRadioButton = findPermutateRadioButton();
if (radioButtons != null && radioButtons.getButtonCount() == 0) {
radioButtons.add(combineRadioButton);
radioButtons.add(permutateRadioButton);
combineRadioButton.setSelected(true);
checkRadioButton(combineRadioButton);
combineRadioButton.repaint();
}
radioButtonsArePrepared = true;
}
}

Shai Almog

unread,
Aug 29, 2014, 1:16:20 AM8/29/14
to codenameone...@googlegroups.com
After the method executes you will notice that radioButtonsArePrepared will be false.
Reply all
Reply to author
Forward
0 new messages