简体   繁体   中英

Enable a textbox when a checkbox is selected

I have a dialog which contains a checkbox and a textbox. The textbox should only be enabled when the checkbox is checked.

Currently I tried doing this with the following binding.

CheckBox cbox = new CheckBox("Enable textbox");
TextBox tbox = new TextBox();
tbox.disableProperty().bind(cbox.selectedProperty());

This however has the exact opposite effect that I want (The textbox becomes disabled when I select the checkbox). And I can't find an "enableProperty" on textbox.

I have found that the textbox has a "editableProperty()", and binding that to the selectedProperty has parts of the desired effect that I want. However, I feel disabling would still be a better option, as it's visually more intuitive.

It's probably an easy fix, but how exactly do I have to bind the properties to enable the textbox when the checkbox is selected?

tbox.disableProperty().bind(cbox.selectedProperty());

That means when the cbox is selected the tbox will disable. You can use the not property:

tbox.disableProperty().bind(cbox.selectedProperty().not());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM