Search code examples
javacomponentscodenameone

Codename One get and disable all components and sub components


I have an Android application with several containers and other components like buttons and labels.

The app uses popups to inform the user on the first run about the usage of the app and its components.

I want to disable the form and/ or all the components and sub components inside, until the users presses the close button of the popup dialog.

I have tried using form.setEnabled (false), but that didn't work.

form.getComponentCount () 

also only gets the containers and the toolbar and not what's inside the containers, so it is insufficient.

Now I use the following code to disable the components :

form.setScrollable (false); 

for (Component component : containerOne ) {
        component.setEnabled(false);
}

for (Component component : containerTwo ) {
        component.setEnabled(false);
}

for (Component component : containerThree ) {
        component.setEnabled(false);
}

buttonOne.setEnabled(false);
buttonTwo.setEnabled(false);

textFieldOne.setEnabled(false);
textFieldTwo.setEnabled(false); 

but this takes a lot of code, since I need to enable the components later and I am also using several classes.

I there a one-liner or easier way to achieve this?


Solution

  • You can use the ComponentSelector to enable/disable components in one line of code.

    $("*", rootContainer).setEnabled(false)
    
    1. The $ is an alias of ComponentSelector.select()
    2. The "*" is a selector (similar to a CSS selector) that says to match all components.
    3. The rootContainer is a Container object that serves as the root to being selection on.
    4. setEnabled(false) sets enabled=false on all Components in the found set.