Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

dispose() not disposing  RSS feed

 
ibrahim yener
Ranch Hand
Posts: 202
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was using All form elements in same class before therefor, it made me do same thing over and over again.
So, i took Tarun Bolla suggestion at http://www.coderanch.com/t/620921/GUI/java/retrieving-textField-approach#2837119 and put all buttons into different class.

Here is my problem start, Button onclick event doesn't dispose window.

Here is my sample code

My main Frame


And Button class


How can i dispose current frame when clicking some button to open another frame.

Regards
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your dispose doesn't access to the Frame to call dispose on it. If you really need to do this then you can make the frame available in your GuiButtons class by passing it into its constructor.
You should however prefer to keep your frame alive and just remove or dispose dialogs and panels that you add to it.
 
Bernhard Goetz
Ranch Hand
Posts: 57
1
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, if you need some basic help about Swing gui creation I'd like to suggest you the official Java Tutorial pages.

On a side note: Try to name your classes as nouns and your methods as verbs. The class PlaySound should rather be a class called SoundPlayer with a method playSound().
 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the following to get access to the window that you want to dispose:

 
ibrahim yener
Ranch Hand
Posts: 202
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply.

E Armitage and Rob Camick can i get some example please. I have no idea how to deal with what you guys said since i am a java student.

Regards
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the structure of your code. You want to dispose the frame inside the GuiButtons class. However, you don't have a (direct) reference to that frame so you can't just call frame.dispose(); because GuiButtons doesn't have the frame reference to call the method from. So you can either make the frame reference available to the GuiButtons class by passing it into it's constructor like I suggested or better use Rob's suggestion which uses the buttons that you have in the GuiButtons to get hold of the frame by asking those buttons what their window is. We know that those buttons are on the frame so we can ask them for the frame and then we can call dispose on the frame once we get it from the buttons.
Rob's approach is better because it gets the job done without adding extra dependencies between your classes, also Rob uses those swing classes as minions that he commands at will.
 
ibrahim yener
Ranch Hand
Posts: 202
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Armitage
Thanks for reply.

The problem is i do not know what to put inside of parenthesis of Rod's code

Window window = SwingUtilities.windowForComponent(...);

those tiny dots still remain unknown from my side.
 
Campbell Ritchie
Sheriff
Posts: 53749
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the documentation for that method?
 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I purposely use (...) to force you to read the API to find out what parameters are needed for this method. This way not only to you learn how to use this method, but if you read the read of the API you may also find other methods in the class that will be helpful in the future to solve other problems.

It is a long process to learn Java. Sometimes you need to do it one class at a time as you are introduced to new classes.

Or if you still don't understand how to use the method, you can always search the forum/web for examples that use the method.
 
ibrahim yener
Ranch Hand
Posts: 202
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, i solved my problem.

Rob, thanks for forcing me to search a solution.
after 3 days of search, i finally found the solution how to handle window from other classes.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!