When I say objects here, I'm referring to the Swing objects that correspond to "objects" on the screen.

I'm using NetBeans and the "main" class for this program is quite lengthy (for the simple stuff I'm doing) and so hopefully the scaled down version I have below will suffice.

So, as you know, there's a class in which is the main window of the application. I have a couple of buttons for this window, one of which opens the graphic file that I'm testing with. The mouse clicking event calls a method, which is pasted below.

private void DisplayImageMouseClicked(java.awt.event.MouseEvent evt) {
BufferedImage buffImg;

try {
buffImg = ImageIO.read(new File(imageFilePath.getText()));
PhotoViewer myPV = new PhotoViewer(buffImg);


catch(IOException ioe) {
"I/O Exception reading image",
catch(NullPointerException npe) {
StringBuilder mySB = new StringBuilder();
for(StackTraceElement ste : npe.getStackTrace()) {
"NULL Pointer",

I think I'm having some problems with understanding how to properly use Swing components with the scope and visibility rules of Java. When this method is called, it makes a new object of type "PhotoViewer" and adds it to the main panel of the program. (Incidentally, this mainPanel object I'm pretty sure is a JPanel contained within the main container object of the program. Don't know if that's relevant or not.)

Now, I would think that once the method above exits, the object instantiated in this method goes out of scope and is no loner visible to the program. However, minimizing and maximizing the window shows that the overridden paintComponent() are properly being called. Thus, it would seem that the object persists though the method which invoked it is not present on the stack. Is this because PhotoViewer is a reference type and has been allocated on the stack?

If I'm correct in all of this, how do I make new graphic images paint? I can paint the first image, but when I try to paint another image, the first image remains. The only explanation I've come up with so far is this.

I'd truly appreciate some input on this.

Thanks everyone,