In Swing, when you disable a component using the setEnabled() method, only the current component will be disabled. Any children found within that component will not be affected. I searched in Google for some workarounds that can fix this. A GlassPane did not do the trick. Well, it may have worked if it was a JFrame that was involved but I wanted a JPanel to be disabled, not the whole application window.

Using the JPanel’s JRootPane‘s GlassPane did not do the trick either. I also did not want to be in a scenario where I had to loop through all children just to disable them. As a last resort, I checked out JXLayer and boy, was it so easy to implement. Plus, it is still lightweight too and your existing code will not be affected.

To use JXLayer, follow the code below.

Pretty easy right? You just need to set setLocked() to true or false if you want the panel to be disabled or not. JXLayer is a very handy tool for intercepting keyboard and mouse events on a component.

Related Posts Plugin for WordPress, Blogger...