tags:

views:

228

answers:

4

I'm looking for a good way to disable a JPanel. I'm using a MVC design for a Java Swing GUI. I want the JPanel to be disabled while the model is processing stuff. I've tried setEnabled(false). That disables user input on the JPanel, but I'd like it to be grayed out to add a more visual effect.

Thanks in advance!

+1  A: 

JPanel doesn't appear any different when disabled, you'll have to override the paintComponent() method to draw it differently (or with a different color) when it is disabled. Something like this might work:

protected void paintComponent(Graphics g) {
    if (this.isOpaque()) {
        Color color = (this.isEnabled()) ? this.getBackground() : this.getBackground().brighter();
        g.setColor(color);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
    }
}
Kylar
+1, Except you can probably just call setBackground(color).
Chris
A: 

Since you want to apply a visual effect its better to use Glasspane. Check this article. SwingX already provides the components you need and much more. Check out the demo on the site for various components available.

+2  A: 

Have you looked into Glass Panes? They are useful for painting over areas which already contain components. Take a look

billynomates
A: 

Another solution is to use JXLayer framework. It is much more flexible then glass pane. Take a look at the project and this article

eugener