Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6541351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:05:40+00:00 2026-05-25T11:05:40+00:00

In the following code, I call JOptionPane.showMessageDialog, inside a try/catch block. But when the

  • 0

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);
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:05:41+00:00Added an answer on May 25, 2026 at 11:05 am

    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;
       }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got a little problem. Sometimes, when I try to call the following code,
I'm using the following code block to call a SQL Server 2005 stored procedure.
i am using following code to make an httpPost call but it is returning
I am using the following code to call a javascript function but the OnClientClick
I have the following c# code to call stored procedure testproc , but when
I'm using the following code to call a TaskDialog. [DllImport(ComCtl32, CharSet = CharSet.Unicode, PreserveSig
I'm using he following code the call a CFC which returns auto-suggest results through
I am using the following code to call a batch file: dim shell set
If I step through the following code the call to ReturnOne() is skipped. static
With the following code the analyzer marks the setMyDict selector call as a potential

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.