JOptionPane.showMessageDialog() shows but without any message?

11,625

Solution 1

It's likely a Swing concurrency issue. But more importantly, you should never load an image from within a paint or paintComponent method, ever. Read it in the constructor or elsewhere but paint/paintComponent need to be lean and blazingly fast.

To solve your issue, consider reading in the image in SwingWorker object. If you call a JOptionPane from within the SwingWorker's doInBackground method though, be sure to call it on the Swing event thread, the EDT, using SwingUtilities.invokeLater(Runnable).

Also, you will hardly ever want to draw in a JPanel's paint method unless you are taking care of painting borders and children. Instead paint in a paintComponent method, and don't forget to call the super.paintComponent(g) method in that paintComponent override. You'll want to read the Swing graphics tutorials as this is all spelled out there.

For example:

import java.awt.Graphics;
import java.awt.Image;
import java.io.InputStream;
import java.util.concurrent.ExecutionException;

import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

public class MyBoardPannel extends JPanel {
   protected static final String SAMPLE_IMAGE = "sampleImage";
   Image imageToShow = null;

   public MyBoardPannel() {
      SwingWorker<Image, Void> mySW = new SwingWorker<Image, Void>() {

         @Override
         protected Image doInBackground() throws Exception {
            return loadImage(SAMPLE_IMAGE);
         }

         @Override
         protected void done() {
            try {
               imageToShow = get();
            } catch (InterruptedException e) {
               e.printStackTrace();
            } catch (ExecutionException e) {
               e.printStackTrace();
            }
         }
      };

      mySW.execute();
   }

   @Override
   public void paintComponent(Graphics grahics) {
      super.paintComponent(grahics);
      if (imageToShow != null) {
         grahics.drawImage(imageToShow, 0, 0, null);
      }
   }

   private Image loadImage(String imageName) {
      InputStream imageStream = getClass().getResourceAsStream(
            "/" + imageName + ".png");
      try {
         return ImageIO.read(imageStream);
      } catch (Exception e) {
         final String errorMessage = "Failed to load image " + imageName;
         System.err.println(errorMessage);
         SwingUtilities.invokeLater(new Runnable() {
            public void run() {
               JOptionPane.showMessageDialog(MyBoardPannel.this, errorMessage,
                     "Image loading error", JOptionPane.ERROR_MESSAGE);
               System.exit(1);
            }
         });
      }

      return null;
   }

}

Solution 2

I don't really know, but maybe your panel you use as parent of the JOptionPane (by passing this) is invisible or there is something else wrong. Try adding pack(); at the end of your JOptionPaneErrorShowing constructor.

What I know is that I had this problem when I was using an old Ubuntu and old Nvidia driver for my GPU, when the desktop effects were turned on (the Compiz Fusion of today. I don't know if it was already called Compiz, that long ago).


Aha! I found it, you are displaying the error inside the repaint method. Never do that! Load your image inside the constructor of the MyBoardPanel class and show error messages over there.

Share:
11,625
loloof64
Author by

loloof64

I love programming, mainly as an hobbyist.

Updated on August 29, 2022

Comments

  • loloof64
    loloof64 over 1 year

    In the following code, I call JOptionPane.showMessageDialog, inside a try/catch block. But when the error is caught, my JOptionPane is visible but without any message !!! Does someone knows why and how I can correct the problem ?

    Regards

    MyBoardJPannel.java

    package experimentations.gui;
    
    import java.awt.Graphics;
    import java.awt.Image;
    import java.io.InputStream;
    
    import javax.imageio.ImageIO;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    
    public class MyBoardPannel extends JPanel {
    
    @Override
    public void paint(Graphics grahics) {
                if (imageToShow == null)
            imageToShow = loadImage("sampleImage");
    }
    
    /**
     * In fact, there are not any image in project => will go to catch clause.
     * @param imageName
     */
    private void loadImage(String imageName) {
        InputStream imageStream = getClass().getResourceAsStream("/"+imageName+".png");
        try {
            imageToShow = ImageIO.read(imageStream);
        }
        catch (Exception e) {
            String errorMessage = "Failed to load image "+imageName;
            System.err.println(errorMessage);
            JOptionPane.showMessageDialog(this, errorMessage,
                    "Image loading error", JOptionPane.ERROR_MESSAGE);
            imageToShow = null;
            System.exit(1);
        }
    }
    
    private Image imageToShow;
    
    
    
    }
    

    JOptionPaneErrorShowing.java

    package experimentations.gui;
    
    import javax.swing.JFrame;
    
    public class JOptionPaneErrorShowing extends JFrame {
    
    public JOptionPaneErrorShowing(){
        setTitle("JOptionPane experimentation");
        setSize(300, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    
        add(new MyBoardPannel());
    }
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        new JOptionPaneErrorShowing().setVisible(true);
    }
    
    }
    
  • loloof64
    loloof64 over 12 years
    Thank you very much :) I did not know about such Swing issue
  • Martijn Courteaux
    Martijn Courteaux over 12 years
    It is not a Swing issue. It is simply you are not allowed to do. It is totally wrong to do it. When you advance in programming with Java and Swing, you will understand. :D
  • Hovercraft Full Of Eels
    Hovercraft Full Of Eels over 12 years
    You in fact are allowed to download images inside of the paint or paintComponent method; it's just a very very bad idea as it slows the GUI's drawing to a crawl and makes the GUI almost unresponsive.
  • loloof64
    loloof64 over 12 years
    Thank you very much :) Though I knew it's a good practice to isolate long task from UI Thread, I did not take care of this in the current case.