JPanel Drop Shadow

Get Off My Lawn picture Get Off My Lawn · Nov 13, 2012 · Viewed 23.3k times · Source

I have a JPanel element and I would like added a drop shadow to it, how can I add a nice faded drop shadow to the element? Do I need to use external libraries or is there something that is built in that I can use?

Example:

enter image description here

Answer

Get Off My Lawn picture Get Off My Lawn · Nov 13, 2012

So I looked into swingx which extends JPanel and was able to achieve the results I was looking for with the following code:

public class Canvas extends JXPanel{

    public Canvas(){
        DropShadowBorder shadow = new DropShadowBorder();
        shadow.setShadowColor(Color.BLACK);
        shadow.setShowLeftShadow(true);
        shadow.setShowRightShadow(true);
        shadow.setShowBottomShadow(true);
        shadow.setShowTopShadow(true);
        this.setBorder(shadow);
    }
}

And the result:

Dropshadow