Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Swing / AWT / SWT and the fly likes Help with response function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Help with response function" Watch "Help with response function" New topic

Help with response function

Chris Swanson

Joined: Nov 30, 2009
Posts: 15
Hello, I am new to swing, and am having trouble on performing some object oriented functionality. I have two classes. When I hit the response button in testPanel, I want the testPanel to dissapear, and I want main to be notified of the change. Is there any way to do this?

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10651

Welcome to the Ranch.

What you need is a ContainerListener

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Chris Swanson

Joined: Nov 30, 2009
Posts: 15
Ok, so I learned how to use a container listener. Now I am trying to make this work, and while it compiles and runs, when the container listener goes off and removes the inner class, it fires a bunch of errors.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

What am I doing wrong with my code? I know how to make the main.remove(clusterMain); line of code to fire once, but I am still getting errors during run time. What needs to be changed?

Outer Class

Inner class

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281

java amateur
Rob Spoor

Joined: Oct 27, 2005
Posts: 19908

Chris, when do you get this exception? Immediately at the start? That would make sense if you add this listener to the container before you are done creating your GUI; if you add a component in the constructor while either main, clusterMain or panel is still null the listener will still be triggered. Don't add the listener until you are done building your GUI.

If it happens afterwards, make sure these three fields are not null. If your code needs to allow them to be null, surround the code with null checks:

How To Ask Questions How To Answer Questions
Consider Paul's rocket mass heater.
subject: Help with response function