• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with response function

 
Chris Swanson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris,
Welcome to the Ranch.

What you need is a ContainerListener
http://java.sun.com/docs/books/tutorial/uiswing/events/containerlistener.html

 
Chris Swanson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic