It's not a secret anymore!
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 Make it so: Java DB Connections & Transactions this week in the JDBC 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: 10771

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: 20202

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
I agree. Here's the link:
subject: Help with response function
It's not a secret anymore!