I have a Composite
that I want to be able to enable/disable programatically. The Control.setEnabled(boolean enabled)
method works fine, but it does not give any visual information that the widget(s) are disabled.
What I would like to do is to have the disabled state mean the widgets are greyed out. Right now they just enter a weird state where the user is unable to click or perform any action on them.
In other words, you need to write code like this, given a
Composite c
:The other solutions posted here are pretty primitive. They have several shortcomings:
The code below solves these problems. It is the ultimate enabler/disabler for SWT.
It keeps track of the modified controls by tagging them with
Widget.setData
, so that it only enables the contols it previously has disabled. It handles different kinds of controls differently in tree states:DISABLED
,READ_ONLY
andEDITABLE
.One limitation of this is that even in disabled state it is still possible to change the active tab in a
TabFolder
control.The problem was indeed that I was disabling the composite and not the controls inside it. What I ended up doing was something like this:
A Composite is a container control that hold other controls using a layout - you can't see a composite really, you can only see the controls it holds. To disable and visually see then disabled, you'll have to call
setEnabled(false)
on all the children, assuming they're not containers too. Basically, to have to enable/disable the leaf widgets and you will see visual indication.The reason you can't do anything with the widgets when disabling the Composite is because the Composite is eating all the events. Although the child widgets are not getting the events forwarded, they know nothing about the state of their parent, so they aren't greyed out.