Disabled Checkbox or DDL in Nifty

How can I disabled a controller? say a check box in nifty? There’s nothing in the API that mentions that imp functionality.

I would like the checkbox to still appear but greyed out showing the user that he’s not allowed to click on it.

thanks guys

I’m not aware of any disable functionality built into the standard Nifty controls. However, I believe you can gray out the checkbox by changing its color on the fly and disable it by ignoring the events it generates.

Not that doing so is hard, but it’s counter intuitive. There must be a way to disable the controllers in a more practical way.

@void256, is he right?

I’m not aware of any disable functionality built into the standard Nifty controls. However, I believe you can gray out the checkbox by changing its color on the fly and disable it by ignoring the events it generates.

I take it back @sgold, even that seems not work.

I tried doing s.color(Color.Black) or selectionColor() or hide() and all of them don’t seem to do anything. where s is my CheckboxBuilder instance.

how do u change the color on the fly?

All Nifty standard controls implement the NiftyControl interface which has these beautiful enable() and disable() methods :wink:

Even the core Nifty Element class has these enable() and disable() methods.

You can use the onEnabled and onDisabled effects if you want to add it to your own custom controls too. Here is the nifty-checkbox.xml from the default black style that shows how the CheckBox control does it.

actually looking further in the aPI there’s a setEnabled(false);

that does the job but it diabled the whole window (i.e. I won’t be able to exit the window )

What you can’t do at the moment is to create a Element/Control disabled. They will always be enabled by default. You have to disable them in your ScreenControllers or Controllers onStartScreen() method. Or somewhere else before the Screen/Control gets active.

@void256 , Not sure I follow… so is something like that doable at all?

I have a menu where the user chooses which mode he wants to be in (Mode A or Mode B) , if mode A is chosen do not disable anything and just allow everything to be togglable. IF Mode B is chosen, then SOME of the toggles are turned off / disabled. That’s when I do the check

[java]
if (modeChosen == Mode.B)
{
greyOut(toggleId);
}

// disable the checkboxes relative to
// the modes that don’t have access to them
private void greyOut(Set<CheckboxBuilder> checkboxSet) {
for (CheckboxBuilder s : checkboxSet) {
setEnabled(false);

    }
} [/java]

and for now this works except that the whole window gets disabled >> which means I cannot close it anymore AND it disables ALL of the checkboxes within the window (I only want to disable SOME of them)

PS: I am using the java builder and not xml

Also I realized I am using:

[java]
control(new CheckboxBuilder(“test”) {{
valignTop();
checked(true);
}}); [/java]

instead of [java]CheckBox checkBoxControl = screen.findNiftyControl(“checkbox”, CheckBox.class);[/java]

which is why I don’t have access to the setEnabled() and other itnerface controls you’re talking about. I never used Nifty Control before but use the Java builders and controllers instead. Is that only for xml nifty? If not, Could you maybe refer to an examle code or just let me know how I Can get the NiftyControl to work? I can’t get to attach it to Nifty after creating it (cz I dont see the checkbox when I run it the app)

After you have build your Screen using the Builder you have the same runtime representation as when you would have load it from XML. So after you have a screen you could access all the controls you’ve created with a call to screen.findNiftyControl() for instance:

[java]CheckBox checkBoxControl = screen.findNiftyControl(“test”, CheckBox.class);
checkBoxControl.disable()[/java]

1 Like