views:

188

answers:

1

How to set background Image of JOptionPane? I want to show a different image on background of an JOptionPane.

A: 

You could extend the JOptionPane class and override the paint method.

Edit: Depending on the image resolution and quality you might be able to stretch it with an AffineTransform during the WindowResize events without to much distortion. This would let you handle the JOptionPane and image size discrepancies mentioned below.

   class ImageBackgroundPane extends JOptionPane
    {
         private BufferedImage img;

         public ImageBackgroundPane (BufferedImage image)
         {
            this.img = image;
         }

         @Override
         public void paint(Graphics g)
         {
           //Pick one of the two painting methods below.

           //Option 1:
           //Define the bounding region to paint based on image size.
           //Be careful, if the image is smaller than the JOptionPane size you
           //will see a solid white background where the image does not reach.
           g.drawImage(img, 0, 0, img.getWidth(), img.getHeight());

           //Option 2:
           //If the image can be guaranteed to be larger than the JOptionPane's size
           Dimension curSize = this.getSize();
           g.drawImage(img, 0, 0, curSize.width, curSize.height, null);


           //Make sure to paint all the other properties of Swing components.
           super.paint(g);
         }
    }
Tansir1
Does not worked... Can you please give me a running example?
Aizaz