title: Java Swing: Enabling/Disabling all components in JPanel
body: <p>I have a JPanel which contains a JToolbar (including few buttons without text) and a JTable and I need to enable/disable (make internal widgets not clickable). I tried this:</p>
  
  <pre><code> JPanel panel = ....;
   for (Component c : panel.getComponents()) c.setEnabled(enabled);
  </code></pre>
  
  <p>but it doesn't work.  Is there a better and more generic solution to enable/disable all internal components in a JPanel?</p>
  
  <p>I have partially solved my problem using JLayer starting from the example here <a href="http://docs.oracle.com/javase/tutorial/uiswing/misc/jlayer.html" rel="nofollow noreferrer">http://docs.oracle.com/javase/tutorial/uiswing/misc/jlayer.html</a>:</p>
  
  <pre><code>layer = new JLayer&lt;JComponent&gt;(myPanel, new BlurLayerUI(false));
  .....
  ((BlurLayerUI)layer.getUI()).blur(...); // switch blur on/off
  
  
  class BlurLayerUI extends LayerUI&lt;JComponent&gt; {
    private BufferedImage mOffscreenImage;
    private BufferedImageOp mOperation;
  
    private boolean blur;
  
    public BlurLayerUI(boolean blur) {
        this.blur = blur;
        float ninth = 1.0f / 9.0f;
          float[] blurKernel = {
            ninth, ninth, ninth,
            ninth, ninth, ninth,
            ninth, ninth, ninth
          };
          mOperation = new ConvolveOp(
                  new Kernel(3, 3, blurKernel),
                  ConvolveOp.EDGE_NO_OP, null);
          }
  
    public void blur(boolean blur) {
        this.blur=blur;
      firePropertyChange("blur", 0, 1);
     }
  
    @Override
    public void paint (Graphics g, JComponent c) {
        if (!blur) {
              super.paint (g, c);
              return;
          }
  
        int w = c.getWidth();
      int h = c.getHeight();
  
  
  
      if (w == 0 || h == 0) {
        return;
      }
  
      // Only create the offscreen image if the one we have
      // is the wrong size.
      if (mOffscreenImage == null ||
              mOffscreenImage.getWidth() != w ||
              mOffscreenImage.getHeight() != h) {
        mOffscreenImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
      }
  
      Graphics2D ig2 = mOffscreenImage.createGraphics();
      ig2.setClip(g.getClip());
      super.paint(ig2, c);
      ig2.dispose();
  
      Graphics2D g2 = (Graphics2D)g;
      g2.drawImage(mOffscreenImage, mOperation, 0, 0);
    }
  
    @Override
    public void applyPropertyChange(PropertyChangeEvent pce, JLayer l) {
      if ("blur".equals(pce.getPropertyName())) {
        l.repaint();
      }
    }
  
  }
  </code></pre>
  
  <p>I still have 2 problems:</p>
  
  <ol>
  <li><p>In the link above events are relative to mouse only. How can I manage the keyboard events?</p></li>
  <li><p>How can I create a "gray out" effect in place of blur?</p></li>
  </ol>