As an alternative to the above solution I adopted this another practice.. I created a class the initializes a frame and then called this frame from another class... The problem however surprisingly remained the very same.

We've discussed this before, you and I. Never, never call JOptionPane from inside of paint/paintComponent. Period. You cannot control when this method gets called. This method should be small and fast and never slowed down by user interaction. This is not a problem to resolve other than to never get user interaction from inside of this method.

The paintComponent( ... ) method is called whenever 'something' (the AWT EDT thread) thinks the component needs to be repainted. That method is called often and often at moments you didn't expect it to be called. So, don't show a JOptionPane in the body of that method. You have to take that logic out of the method and show it when needed.

the painComponent() method calls a method that throws exception.. that is handled using try-catch inside the paintComponent().. that's ok i won't call JOptionPane or Frame or other components from inside the Paint method. but when an exception is caught inside the paint i need to inform the user about it using JOptionPane or any other component..

How can i exactly inform the user about the exception been caught while the exception is handled inside the paint method.?? plz suggest.:confused:

I agree with Fubarable and JosAH. This piece of code certainly is strange, however there are times that one would like user input in any situation. I tried to create a simple console for Java, and faced a similar problem. I thought the output the OP's code gives was cool. ;) But anyways:

Why do you need to catch an exception in paintComponent? Exceptions should be caught from errors in program logic, and paintComponent should be free of program logic. Please go into that in a bit more detail as I suspect that your program has a faulty design. Perhaps we can help you fix it. You may need to create an SSCCE that shows why the try/catch is necessary.

the painComponent() method calls a method that throws exception.. that is handled using try-catch inside the paintComponent().. that's ok i won't call JOptionPane or Frame or other components from inside the Paint method. but when an exception is caught inside the paint i need to inform the user about it using JOptionPane or any other component..

How can i exactly inform the user about the exception been caught while the exception is handled inside the paint method.?? plz suggest.:confused:

You're dealing with the classic producer/consumer problem: something produces an error (the producer) and something else (the consumer) is waiting for something to be produced. As long as nothing is produced the consumer waits; when something has been produced the producer notifies anything else that is waiting for it.

I am actually plotting a graph inside the paintComponent method. and I need to replot it every time the window appears. Now taking the logic outside the paintComponent method would be quite typical perhaps.. beacuase as i mentioned i need to replot it.

and within the logic an exception is thrown and handled .. and i need to inform the user for the same.suggest.

Well, a graph typically consists out of primitives: polylines, polygons, ellipses, images and so on. Let a method outside of the paint method do the graph construction outputting a structure of primitives and just draw that structure in your paint method. You can remove all logic from your paint method this way, and only worry about painting. That's what I would do. ;) Use a window event to call the method when the window is shown.

Eyes dwelling into the past are blind to what lies in the future. Step carefully.

No, this is a very bad design indeed as you're doing what you've been told never to do: never do program logic inside of paintComponent. Never. Simply don't do it. Instead, plot your graph in your regular program and place the plotted points into a collection. Call repaint() when you want the GUI to display the plot or the portion of the plot, and then have paintComponent use collection of points to draw the graph.

and I need to replot it every time the window appears. Now taking the logic outside the paintComponent method would be quite typical perhaps.. beacuase as i mentioned i need to replot it.

Bad solution though. Use a focus listener or a WindowListener (perhaps better), but (again): Let me repeat what I stated above: never do program logic inside of paintComponent. Never.