1

My problem is that when I create a message dialog using

JOptionPane.showMessageDialog( ... )

On an application that displays a JPanel that draws an image as background (taken from: java swing: how to add an image to a jpanel), the backgrund image is not displayed, so I have to minimize and maximize the application to get the background image back.

So far I can only get back the background image by doing something like this:

app.getApplication().getMainFrame().repaint();

but it only works after I close the Message Dialog.

Any ideas?

Community
  • 1
  • 1
user935581
  • 17
  • 5
  • 1
    *"Any ideas?"* For better help sooner, post an [SSCCE](http://sscce.org/). For an SSCCE related to image(s), either [generate them in code](http://stackoverflow.com/questions/5621338/about-swing-and-jtable/5630271#5630271) or [hot-link](http://pscode.org/media/#image) to them. – Andrew Thompson Feb 01 '12 at 05:43
  • I found out this error only happens when the JOptionPane is modal. – user935581 Feb 05 '12 at 16:35
  • Change `null` to `this` and it should work for a modal dialog. If not, post an SSCCE. – Andrew Thompson Feb 05 '12 at 23:27

1 Answers1

3

The link you had posted, in that he is accessing Image with File, which seems to me as not that good a way to access Application Resources, for that you must use URL. Have a look at this sample code, and check where you going wrong with this :

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;    
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

public class ImageTest extends JPanel 
{
    private BufferedImage image;

    private void displayGUI()
    {
        JFrame frame = new JFrame("Image Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        try
        {
            setImage(new URL("http://gagandeepbali.uk.to/" +
                "gaganisonline/images/planetbackground.jpg"));
        }
        catch(MalformedURLException mue)    
        {
            mue.printStackTrace();
        }


        frame.setContentPane(this);
        frame.pack();
        frame.setVisible(true);        
        JOptionPane.showMessageDialog(frame, 
                                "I am working.", 
                                "Image Working ?", 
                                JOptionPane.QUESTION_MESSAGE);
    }

    private void setImage(URL path)
    {
        try
        {       
            System.out.println(path);
            image = ImageIO.read(path);
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
        }
    }

    @Override
    public Dimension getPreferredSize()
    {
        return (new Dimension(image.getWidth(), image.getHeight()));
    }

    @Override   
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, this);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new ImageTest().displayGUI();
            }
        });
    }
}
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • invalid implementation of paintComponent (remember: a JPanel's opaque property is true by default :-) – kleopatra Aug 13 '12 at 06:33
  • `JPanel` is really not opaque for every `LookAndFeel`. For some LookAndFeel it is not. – nIcE cOw Aug 13 '12 at 06:56
  • so what - because it accidentally not violates its contract in some odd LAFs it's correct to let it violate in others? -1 – kleopatra Aug 13 '12 at 07:02